DAKAR, Senegal — Barack Obama had been a United States senator for just weeks in early 2005 when Oprah Winfrey offered to carry a message for him to Nelson Mandela, the iconic South African leader.
Mr. Obama disappeared into a back room in Ms. Winfrey’s television studio to write the note, but he was gone so long that his spokesman, Robert Gibbs, popped his head in after half an hour.
“You’ve got to give me some time here,” Mr. Obama, pen in hand, told Mr. Gibbs, who recalled the moment recently. “I can’t just wing a note to Nelson Mandela.”
Mr. Obama had been hoping to have his first face-to-face meeting as president with the ailing 94-year-old leader during a three-country, weeklong trip to Africa that began on Wednesday. But Mr. Mandela was hospitalized on June 8 for a chronic lung infection, and on Thursday he remained in critical but stable condition.
A meeting between Mr. Mandela and Mr. Obama would have been rich with symbolism and symmetry for people on both continents: two men from different generations who made history as the first black presidents of nations with deep racial divides. Both embraced a cool pragmatism in their attempt to be post-racial leaders, and both have inspired as well as disappointed many supporters.
Mr. Mandela has long been a beacon for Mr. Obama, who recounted again on Thursday how the revolution unleashed by Mr. Mandela a world away had inspired his own activism. Friends of Mr. Obama say that for him and many of his contemporaries, the fight against apartheid was the equivalent of the civil rights movement of an earlier generation.
“My first act of political activism was when I was at Occidental College,” Mr. Obama said Thursday at a news conference here in Dakar, referring to a brief speech he gave at the time. “I got involved in the anti-apartheid movement back in 1979-80 because I was inspired by what was taking place in South Africa.”
Recalling Mr. Mandela’s long imprisonment, Mr. Obama said, “I didn’t necessarily imagine that Nelson Mandela might be released.”
Mr. Obama rarely dwells on his own strides against racial barriers and has for the most part steered away from invoking Mr. Mandela’s struggles in public or in private. Aides say he considers it presumptuous to compare his own life to that of Mr. Mandela, who was imprisoned for 27 years, part of that time in a tiny cell on Robben Island, before forging an end to apartheid and becoming South Africa’s first black leader.
But Mr. Obama’s closest advisers say people do not realize how much Mr. Mandela has been an inspiration to Mr. Obama in some of the president’s most difficult moments. Valerie B. Jarrett, a senior adviser and close friend of Mr. Obama’s, said Mr. Mandela had given Mr. Obama “the strength to persevere.”
In the foreword that Mr. Obama wrote to Mr. Mandela’s 2010 book, “Conversations With Myself,” he describes the early impact that Mr. Mandela’s struggle had on his life and his entry into politics.
“His sacrifice was so great that it called upon people everywhere to do what they could on behalf of human progress,” Mr. Obama wrote. “In the most modest of ways, I was one of those people who tried to answer his call.”
Mr. Obama hinted in the foreword at what might be his most important lesson from Mr. Mandela’s struggles, the need to be stubborn in the face of obstacles. Like Mr. Mandela, who angered some black South Africans who wanted a more radical reordering of the country’s wealth, Mr. Obama has sometimes disillusioned his most ardent supporters.
Mr. Obama’s embrace of some of his predecessor’s antiterrorism policies has frustrated liberals. Revelations about secret surveillance programs have prompted concerns about privacy. And frequent budget clashes with Congress have left those who expected a new tone in Washington disillusioned.
“All of us face days when it can seem like change is hard — days when our opposition and our own imperfections may tempt us to take an easier path that avoids our responsibilities to one another,” Mr. Obama wrote in the foreword. “But even when little sunlight shined into that Robben Island cell, he could see a better future — one worthy of sacrifice.”
In 2006, Mr. Obama stood inside that cell during a visit to South Africa as a senator. After the tour, Mr. Gibbs remembered Mr. Obama as quiet and somber.
“You could see it immediately on his face,” said Mr. Gibbs, who was with Mr. Obama on the trip. “It is almost impossible to comprehend spending that amount of your time imprisoned and in the conditions he was. Nobody really had to say anything. You felt the gravity of history literally on your shoulders.”
During that trip, Mr. Obama did not meet with Mr. Mandela, who was out of the country at the time. The two have met in person only once, in a spontaneous encounter in Washington in 2005, when Mr. Mandela was in town and was urged by advisers to take a few minutes to meet a rising Democratic senator named Barack Obama.
Mr. Obama was in a car, on the way to a meeting, but diverted to the Four Seasons hotel in Georgetown, where Mr. Mandela was staying. The conversation produced a lasting image of Mr. Obama, in silhouette, standing next to a reclining Mr. Mandela.
David Katz, Mr. Obama’s driver and personal aide at the time, took the photo, a copy of which now sits in Mr. Mandela’s foundation office. Another copy is on Mr. Obama’s desk in the Oval Office.
“He was subdued and reflective and honored to have met a man like Nelson Mandela,” Mr. Katz recalled of the young senator’s demeanor after the five-minute exchange. “He was processing the experience, processing the history.”
Mr. Mandela, who was already a retired statesman at the time, expressed admiration for Mr. Obama after meeting him, said his granddaughter Tukwini Mandela. “He thought that President Obama was a very intelligent young man and that he liked him very much,” she said in an e-mail.
Mr. Obama’s engagement in Mr. Mandela’s history began with the speech while he was a student at Occidental College in Los Angeles, where he urged the board of trustees to divest the college of South African investments as a protest against the country’s apartheid policies. At the time, Mr. Mandela was nearing the end of his second decade in prison.
In his brief remarks, Mr. Obama told the crowd that although South Africa’s racial struggles were “happening an ocean away,” it was “a struggle that touches each and every one of us.”
In the few instances since then that Mr. Obama has mentioned Mr. Mandela publicly, it has usually been in conjunction with other renowned leaders. When Mr. Obama accepted the Nobel Peace Prize in 2009, he said, “Compared to some of the giants of history who’ve received this prize — Schweitzer and King, Marshall and Mandela — my accomplishments are slight.”
In a separate interview with Russian television that year, Mr. Obama was asked about his heroes.
“Internationally, Nelson Mandela, Mahatma Gandhi,” Mr. Obama said. “I always am interested in leaders who are able to bring about transformative change without resort to violence, but rather changing people’s minds and people’s hearts.”
Aides said Mr. Obama identified with Mr. Mandela’s willingness to work, pragmatically and without anger or emotion, alongside those who had imprisoned him for so many years.
“The discipline, foresight, endurance — it all really struck a chord,” said Mark Lippert, a longtime foreign policy aide to Mr. Obama who now serves as the chief of staff to Chuck Hagel, the secretary of defense. “He admired Mandela’s ability to rise above it.”
Lydia Polgreen contributed reporting from Johannesburg.