JOHANNESBURG, South Africa — Michael Sata, a former train-station sweeper dubbed the “King Cobra” for his sharp tongue, campaigned for Zambia’s presidency in 2006 on an anti-China platform.

He gained headlines by taking swipes at the Chinese, his country’s biggest foreign investor with $2 billion mainly in copper, cobalt, nickel and coal mines.

Sata referred to Chinese investors as “infesters.” He called for Chinese migrant workers to be expelled from Zambia. And he described Taiwan as a country, breaching Beijing’s obsessive “one China” policy, which considers Taiwan a rogue province rather than an independent nation. China threatened to cut ties with Zambia if Sata won.

It took five years, but Sata finally was elected as Zambia’s president this week on his fourth attempt, throwing a scare into Beijing and delivering a heavy blow to Chinese influence in Africa.

While Sata toned down his criticism of Beijing during this campaign, his anti-China reputation was already well-known in Zambia, where he defeated the pro-China incumbent, Rupiah Banda.

China responded to Banda’s defeat with the same pragmatism as it had toward the loss of friendly regimes in South Sudan and Libya: It tried to befriend the new boss.

"As a friendly country of Zambia, China respects the Zambian people's choice and would like to work with Zambia to promote friendship and expand mutually beneficial cooperation across the board," Chinese foreign ministry spokesman Hong Lei said in Beijing.

But privately, the Chinese government must be worried. Sata has said he may implement capital controls aimed at keeping foreign-exchange earnings in Zambia, Africa’s biggest copper producer and a country that has seen strong economic growth averaging 6 percent over the last three years. Foreign-exchange controls would prevent Chinese companies from sending their profits home to China.

Many of Sata’s supporters are unhappy with the Chinese presence in Zambia. Chinese employers have a reputation for poor working conditions, and Chinese traders have wrested control of some local markets, most notoriously the chicken market, pushing out the Zambian traders.

Zambians were also angry at the Banda government’s failure to prosecute two Chinese managers charged with attempted murder for shooting coal miners during a labor dispute last year.

After his victory, Sata tried to reassure the Chinese.

"Foreign investment is important to Zambia and we will continue to work with foreign investors who are welcome in the country ... but they need to adhere to the labor laws," Sata said after being sworn in as president on Friday.

Despite Sata's assurance, Zambian markets were rocked by his win. The Zambian currency, the kwacha, weakened on Friday, signaling nervousness that Zambia will have a risky investment climate under the populist president with strong opinions on foreign investment.

Sata’s win ends 20 years in power for Banda’s party, the Movement for Multiparty Democracy, and this election is a rare example in Africa of a peaceful transition of political power to an opposition party — the last such instance being the widely praised Ghanaian election of 2009.

Banda, wiping away tears, conceded defeat after receiving only 36 percent of the vote to Sata’s 43 percent, and congratulated his rival.

"The people of Zambia have spoken and we must all listen," Banda said. “Speaking for myself and my party, we will accept the results. We are a democratic party and we know no other way. It is not for us to deny the Zambian people.”

The election this week was marred by violence, most notably by Sata supporters in the country’s Copperbelt mining towns angry over delays in ballot counting and a government ban on the publication of unofficial results.

There were reports that Banda was suspiciously flush with campaign funds, and questions about whether the Chinese were lending a hand: made-in-China lollipops, in wrappers adorned with a pro-Banda slogan, were distributed to voters.

But on the whole, the election was praised for being free and fair. Zambia’s independent Civil Society Election Coalition, which dispatched some 9,000 observers to monitor the Sept. 21 vote, said the polls “generally met international and regional standards for democratic elections.”

“Zambians have been able to exercise their democratic rights and therefore we are all winners regardless of how we voted,” the CSEC said. “Once again Zambia has been able to demonstrate a good example for Africa for democratic elections and peaceful transitions of power.”