“I think there’s been, at times, a disconnect between the way we see the president and how much we love the president and the way some of you perhaps see the president,” Mr. Scaramucci said during a news conference that kicked off with his announcement of Ms. Sanders’s new job.
“I certainly see the American people probably see the president the way I do,” he added, contradicting a raft of recent polls showing Mr. Trump’s approval rating below 40 percent nationally. “But we want to get that message out there.”
In a statement on Friday night, Mr. Trump said, “Anthony is a person I have great respect for,” and went on to describe the problem he hopes he will solve. “We have accomplished so much, and we are being given credit for so little,” he said. “The good news is the people get it, even if the media doesn’t.”
In a tweet late Friday, Mr. Trump also called Mr. Spicer “a wonderful person who took tremendous abuse from the Fake News Media – but his future is bright!”
Mr. Spicer’s departure ends an excruciating saga even for a highly factionalized White House riven with intrigue. He had hoped to last a year as press secretary. He quit after six months and a day.
A former spokesman and strategist for the Republican National Committee, Mr. Spicer was a frequent target of the president’s ire during the first few months of the administration. He attained a notoriety unusual for a presidential spokesman, his combative style spawning a caricature on “Saturday Night Live.”
Mr. Trump expressed gratitude for Mr. Spicer’s service in a statement and predicted that Mr. Spicer will succeed in the future. “Just look at his great television ratings,” he said.
The resignation is a blow to the embattled White House chief of staff, Reince Priebus, the former Republican National Committee chairman who brought Mr. Spicer into the West Wing despite skepticism from Mr. Trump, who initially questioned his loyalty. Mr. Scaramucci described his relationship with Mr. Priebus as a brotherly one where they “rough each other up,” and he called Mr. Priebus a “good friend.”
During the transition, Mr. Trump had planned to appoint Mr. Scaramucci, 52, a Harvard Law School graduate, as the director of his office of public liaison, but the offer was revoked at the request of Mr. Priebus, who had concerns about Mr. Scaramucci’s overseas investments. An aide to Mr. Priebus said he had not blocked it, but merely tried to slow it down.
Mr. Scaramucci’s appointment came two months after the previous communications director, Mike Dubke, stepped down as the official in charge of Mr. Trump’s messaging. The eventual appointment of Mr. Scaramucci was backed by the president’s daughter Ivanka, Mr. Kushner and the commerce secretary, Wilbur Ross, officials said.
Mr. Kushner has grown increasingly critical of both Mr. Spicer and Mr. Priebus, whom he regards as party establishment figures who operate out of self-interest. He also supported Mr. Trump’s decision to supplant Marc E. Kasowitz as his lead attorney on matters pertaining to Russia, according to people familiar with the situation.
Mr. Priebus and Stephen K. Bannon, the president’s chief strategist, both strongly opposed the appointment of Mr. Scaramucci — in large part because he enjoys an easy banter and direct line to Mr. Trump, potentially threatening their positions, four people briefed on the discussions said. People close to both men insisted they had not opposed the move by the president.
Mr. Trump, aggravated by their opposition, dressed the pair down in a testy Oval Office exchange around the time he decided to offer Mr. Scaramucci — known in Trump’s circle as “The Mooch” — the job.
The president, according to three people with knowledge of the situation, said one of the reasons he hired Mr. Scaramucci was to cut down on anonymous leaking — and took a swipe at his two advisers.
He asked them how the leaks were happening, according to a person familiar with the discussions, and called Mr. Spicer a “good guy” who leaks only when told to by Mr. Priebus.
Mr. Scaramucci met with Mr. Priebus on Friday, according to a West Wing official, and the two agreed to work together. When the pair joined Mr. Spicer in the second-floor office of the press secretary in a show of unity, applause could be heard echoing down the hallway.
But senior officials, including Ms. Sanders, were said to be stunned by the sudden shuffle.
While Ms. Sanders will preside at the daily briefing most days, Mr. Trump has told his advisers he is open to rotating new people into the slot. He is said to be especially high on Sebastian Gorka, a blustery foreign policy official who has been accused of having ties to far-right groups in Europe.
During the transition, Mr. Priebus and Mr. Spicer stocked the press shop with their associates from the Republican National Committee, rankling Trump campaign loyalists who reminded the president that Mr. Priebus had suggested he drop out of the race after an “Access Hollywood” tape of Mr. Trump’s comments about women became public.
Mr. Priebus urged Mr. Trump to hire Mr. Spicer and another lieutenant, Katie Walsh, as deputy chief of staff. But Ms. Walsh left the White House after a short time when Mr. Kushner and other West Wing officials forced her out, without Mr. Priebus objecting.
Mr. Trump and Mr. Spicer did not have a close relationship, but the two spent hours together and Mr. Trump said he felt sorry for the ridicule his press secretary received because of his portrayal on “Saturday Night Live.” He repeatedly told aides to convey to Mr. Spicer that he wanted him to stay.
But by the six-month mark of Mr. Trump’s presidency, both men had had enough of each other.
In recent weeks, Mr. Trump had told people that Mr. Spicer was no longer “tough,” one of the harshest insults he can level. And Mr. Spicer told friends he was tired of being blindsided by Mr. Trump, and weary of Mr. Trump’s constant criticism. He instituted the highly contentious practice of holding off-camera briefings, less so to snub reporters than to avoid Mr. Trump’s critiques of his performance, according to one of Mr. Spicer’s friends.
The end came in operatic fashion, befitting Mr. Scaramucci’s namesake — a stock character in Italian musical theater. As Mr. Scaramucci made his big entrance, Mr. Spicer exited quietly, disheartened that the president never quite appreciated his performance.