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.”