A German parliamentary assembly has elected Frank-Walter Steinmeier to become the country’s next president by an overwhelming majority.
Mr Steinmeier, Germany’s former foreign minister, strongly criticised Donald Trump during the US election campaign.
When asked in August about the rise of right-wing populism in Germany and elsewhere, Mr Steinmeier criticised those who “make politics with fear”.
He cited the nationalist Alternative for Germany party, the promoters of Britain’s exit from the European Union, and “the hate preachers, like Donald Trump at the moment in the United States”.
The daily Berliner Morgenpost billed Mr Steinmeier as “the anti-Trump president”.
He was elected with 931 of 1,260 votes. The German president has little executive power but is considered an important moral authority.
"Let's be brave, because then we don't have to be afraid of the future," Mr Steinmeier said in his acceptance speech.
He said the world faces "rough times," but Germany, as a functioning democracy, had the responsibility to fight for stability.
"Isn't it actually wonderful, that this Germany, our difficult fatherland, that this country has become an anchor of hope in the world for many," after overcoming wars and totalitarianism, Mr Steinmeier said.
Chancellor Angela Merkel congratulated Mr Steinmeier and said she was convinced he would be an excellent president who would have the support of the vast majority of the people.
"This is a good day for Germany," she said.
Russian President Vladimir Putin congratulated Mr Steinmeier and invited him to the Kremlin.
Mr Putin president "expressed confidence that Mr Steinmeier’s work as President of Germany will promote Russia-Germany relations and efficient cooperation in various sectors in the interests of the citizens of both nations, in line with reinforcing stability and security on the European continent and globally," a Kremlin press release said.
Foreign secretary Boris Johnson also tweeted his congratulations.
Congratulations to Frank-Walter #Steinmeier on his election as Federal President of Germany— Boris Johnson (@BorisJohnson) February 12, 2017
Mr Steinmeier, a Social Democrat, had the support of Ms Merkel’s “grand coalition” of centre-right and centre-left parties.
He has long been one of Germany’s most popular politicians.
Under Ms Merkel, he served twice as foreign minister – from 2005 to 2009 and again from 2013 until this year, with a stint as opposition leader in between. He has also won respect for his persistence in trying to resolve the long-running crisis in Ukraine.
He will succeed Joachim Gauck, a 77-year-old former pastor and East German pro-democracy activist. He did not seek a second five-year term because of his age.
His election is likely to be one of the last moments of coalition unity ahead of a parliamentary election in September in which Ms Merkel is seeking a fourth term. Both sides hope to end the “grand coalition”.
Additional reporting by AP