After fighting for months in court to try to get a citizenship question on the 2020 Census — and briefly overruling his own Justice Department’s legal surrender — Trump abandoned the effort in a manner that had a familiar plot twist: A surprise backup plan that, in Trump’s view, is actually better than the original plan.

“It’s deeply regrettable, but it will not stop us from collecting the needed information — and I think even in greater detail and more accurately,” Trump said during a Rose Garden news conference Thursday. “Ultimately, this will allow us to have an even more complete count of citizens than through asking the single question alone. It will be, we think, far more accurate.”

It’s part of a pattern that Trump has developed during a presidency in which many of his most hard-fought battles have ended in defeat or awkward pivots to secondary options.

Ever the brand strategist, Trump rarely gives up quietly, instead preferring to invite the cameras in and make the case that his losses are actually unexpected victories. From his failed efforts to repeal Obamacare to Republicans’ loss of the House majority in 2018, Trump has proved adept at seeing — and promoting — the bright side of failure.

The census reversal stands out in part because it played out so dramatically. After a Supreme Court ruling blocked the Trump administration from moving forward with the citizenship question — calling Commerce Secretary Wilbur Ross’s stated rationale “contrived” — officials from the Justice Department and the Commerce Department had said they were giving up their efforts and that the census forms would be printed immediately without a question about citizenship.

Amid conservative backlash, Trump intervened and instructed the Justice Department to reverse its surrender. As recently as last week, Trump said publicly that he had multiple avenues for getting the question on the census.

In the end, he decided to sign an executive order instructing federal agencies to provide information to the Commerce Department about the number of citizens and noncitizens in the country, while leaving the question off the decennial survey.

It’s a far cry from the original goal of asking the millions of census respondents about their citizenship status but, in Trump’s telling, that fallback option will ultimately be better than what he had spent months fighting for.

“I asked, ‘Is there another way?’ And somebody said there’s a way that might be better,” Trump told reporters Friday, when asked why he caved on the citizenship issue. “It might be more accurate. They explained it. I said, ‘Then what are we wasting time — we’re going to be in court for the next two years. What are we wasting time for?’ ”

Since taking office, Trump has had a lot of practice in spinning losses as actual reasons for optimism.

When Trump’s proposal for repealing and replacing Obamacare failed in July 2017, the president fumed publicly and privately about the late Republican senator John McCain’s dramatic midnight vote against it. But he still branded it as a gift in disguise, claiming that the outcome would ultimately work out to his benefit.

“We had one man that after campaigning for eight years, he decided to go thumbs down at two o’clock in the morning,” Trump said during a May rally in Pennsylvania. “That’s all right, but we’ll end up — will end up with even better, because we have plans that are even better than that, so that’s good. Sometimes, when you have adversity, it works out actually better.”

It was the second time Trump had rebranded a failure on health care as an optimistic pitch. When House Republicans pulled their first repeal-and-replace bill in March 2017 after Trump’s efforts to whip votes fell short, the president claimed that the ultimate outcome would be better.

“ObamaCare will explode and we will all get together and piece together a great health care plan for THE PEOPLE,” Trump said in a tweet. “Do not worry!”

Unable to pass a health-care bill in the two years since, Trump has told voters he would sign new health-care legislation in 2021 if he is reelected.

Republicans’ loss of the House majority in 2018 was seen as a major blow to the presidency, as Democrats gained legislative and oversight power in Washington for the first time in Trump’s term.

But the president claimed that keeping the House majority would’ve actually been a worse outcome for him.

“From a dealmaking standpoint, we are all much better off the way it turned out,” Trump said during a news conference the day after the 2018 elections in which Democrats ultimately flipped 40 House seats. “Because I really believe, if the Democrats want to, we can do a tremendous amount of great legislation.”

Trump said maintaining a slim Republican majority would’ve been more challenging, because there would be “plenty of grandstanding” by suddenly emboldened GOP lawmakers with the power to hold legislation hostage.

Pointing to gains in the Senate, Trump described the electoral drubbing as “an incredible day” and “almost a complete victory.”

His remarks stood in contrast to previous presidents, who expressed humility after facing major losses during their midterm elections.

Former president Barack Obama, whose party lost 63 House seats during his first midterm, called the 2010 election a “shellacking.” Former president George W. Bush, whose party lost 31 seats in the House in 2006, called that election a “thumping.”

The optimistic spin is part of Trump’s strategy of casting his presidency as one in which his supporters never get tired of winning, and it could work to motivate his base, said Doug Heye, a former spokesman for Republican House leadership and the Republican National Committee.

“Politically, for his base, he has already won. The thing Trump’s base talks about more than anything is how he ‘fights,’ ” he said. “So as long as he shows that he’s fighting, his base is happy. It’s a rare example of the process being more important than the outcome.”

When Trump traveled to Hanoi, to meet with North Korean leader Kim Jong Un in February, he left the negotiations with no agreement on denuclearization — falling short of the goal he had set for the summit.

Speaking to reporters after the talks abruptly broke down, Trump sought to defy popular belief, saying that, “actually,” his time in Hanoi had been “very productive.”

“I could’ve 100 percent signed something today,” Trump told reporters during a news conference. “We actually had papers ready to be signed, but it just wasn’t appropriate. I want to do it right. I’d much rather do it right than do it fast.”

Trump later said that he’d been given great credit for walking away from the table.

After declaring a national emergency on the southern border — in part because he was unable to secure congressional funding for a border wall — Trump claimed his alternative plan would lead to a quicker resolution.

“I didn’t need to do this, but I’d rather do it much faster,” Trump said in a Rose Garden news conference in February.

On trade, Trump has used tariffs to force trading partners to the negotiating table, saying that he would come out victorious whether he achieved a deal or not.

“We’re getting close to a very historic, monumental deal,” Trump told reporters on May 3 about a trade deal with China. “And if it doesn’t happen, we’ll be fine too. Maybe even better.”

When the deal fell through days later, Trump said that the tariffs were ultimately a better alternative.

“Tariffs will make our Country MUCH STRONGER, not weaker,” Trump said on Twitter on May 10. “Just sit back and watch!”

Trump’s loss of the popular vote during 2016 has also gotten revisionist treatment by the president.

Trump, who previously called the electoral college a “total sham,” has said that his electoral college victory was actually more difficult than winning the popular vote.

“If it was up to the popular vote, I would have been, I think, even better,” Trump said in an interview last month with Chuck Todd of NBC News’s “Meet the Press.” “It’s like you’re training for the 100-yard dash versus the mile.”

The president has cast his administration’s record-high turnover and his inability to get several members of his Cabinet officials confirmed by the Senate as a positive, rather than a negative.

“I sort of like ‘acting,’ ” Trump told reporters in January, referring to the growing number of officials serving in his administration on an acting basis. “It gives me more flexibility.”

Trump’s love for flexibility long predates his time in office. From bankruptcies to market downturns, the former real estate developer has often had to pivot from well-laid plans to adjust to new developments.

“I never get too attached to one deal or one approach,” Trump wrote in his 1987 book, “The Art of the Deal” “For starters, I keep a lot of balls in the air, because most deals fall out, no matter how promising they seem at first.”