Brian Snyder / Reuters

Endurance served John Sidney McCain III nearly as well in politics as it did in war.

McCain, who died on Saturday at age 81 after a year-long struggle with brain cancer, endured more than five years of mistreatment and torture as a prisoner of war in Vietnam, an experience that helped vault him into national politics and endear himself as a hero to the voting public. There he endured scandal and smears, electoral defeats and his own occasional recklessness to make two serious runs at the presidency and earn a place as one of America’s most respected political figures during the last two decades of his life.

Alternately witty and hot-tempered, McCain used candor and accessibility to break through at a time when politicians were closing themselves off to the public and the press. His freewheeling 2000 presidential campaign against George W. Bush positioned him as the Republican front-runner in 2008, when after a roller-coaster candidacy he secured the nomination only to lose to Barack Obama.

During a 31-year career representing Arizona in the Senate, McCain frequently decried the usual politics but proved himself to be a nimble politician: He began as a loyal Ronald Reagan Republican but later cultivated a reputation as a maverick, defying his party at key moments, which earned him kudos from Democrats and independents but vilification from conservatives. There were contradictions, too. In 2008, McCain ran on his decades of experience and his expertise in foreign affairs but selected as his running mate a virtually unknown first-term Alaska governor who had neither.

Few American politicians were more associated with military service than McCain. Born on a naval base near the Panama Canal in 1936, he followed both his father and his grandfather into the Navy, and it was while on a bombing mission in October 1967 that the aircraft he was piloting was shot down over Hanoi.

McCain broke both of his arms and one of his legs in the crash, and he was captured by the North Vietnamese. He’d spend the next five and a half years in a prison that became known as the Hanoi Hilton, where he received little medical treatment and endured beatings and torture at the hands of his captors. The North Vietnamese offered to release him in 1968 as a gesture of good will after McCain’s father was named commander of U.S. forces in the Pacific. But McCain refused on the grounds that the military code of conduct stated that prisoners of war should be released in order of their capture and should not “accept parole or amnesty.”

When McCain refused to go, the guards punished him. “For the next four days, I was beaten every two to three hours by different guards,” he wrote in a 1973 account of his experience. “My left arm was broken again and my ribs were cracked.” He held out for four days before he agreed to sign a “confession” of crimes against the North Vietnamese. “Every man has his breaking point,” he wrote. “I had reached mine.”

McCain came home in 1973 with hundreds of other POWs after President Richard Nixon agreed to finally withdraw U.S. troops from Vietnam. He continued serving in the Navy until 1981, during which time he was assigned as a liaison to the Senate and got a taste for politics. He first won a seat in the House in 1982, having moved from Florida to Arizona after marrying Cindy Hensley, the daughter of a wealthy beer distributor. (McCain’s first marriage broke apart in the years after he returned from Vietnam.)

When opponents accused him of carpetbagging, McCain used his war experience to devastating effect. At a candidate forum, he said he would have loved to have spent his entire life in Arizona, “but I was doing other things. As a matter of fact,” he continued, “when I think about it now, the place I lived longest in my life was Hanoi.”

Four years later, McCain moved up to the Senate, winning the seat vacated by the retiring conservative Barry Goldwater. He would make his name in military and foreign affairs, and by attacking wasteful federal spending. But McCain’s first term in the Senate was tarnished by his involvement in the Keating Five scandal in the late 1980s, in which he and four Democratic colleagues were accused of intervening with federal regulators to try to save a failing savings-and-loan company owned by a top Phoenix donor, Charles Keating. The Senate Ethics Committee cited McCain for “poor judgment” but spared him a more severe rebuke.

Whether McCain’s next act was the work of a chastened convert or a political opportunist remains up for debate. But in the 1990s, the senator, nearly felled by a pay-to-play scandal, became one of the nation’s leading champions of campaign-finance reform—a cause that would establish the one-time Reagan acolyte as a “maverick” willing to challenge his own party and vault him to the upper echelon of American politics. McCain partnered with the liberal Democrat Russ Feingold of Wisconsin to call for limits on so-called soft money and a ban on candidate ads bought by corporations or unions running close to an election.

A version of their bill wouldn’t be enacted until 2002, and the Supreme Court gutted the law in its 2010 Citizens United v. Federal Election Commission decision. But McCain made the issue a centerpiece of his 2000 campaign for the presidency, when he took on front-running Governor George W. Bush of Texas for the Republican nomination. McCain walloped Bush by 18 points in New Hampshire, but the race took a bitter turn in South Carolina. McCain’s wife and adopted daughter were the target of a smear campaign, and push polls accused him of committing treason as a POW in Vietnam. Though Bush’s campaign denied involvement, McCain attacked his rival’s integrity in television ads, comparing him to then-President Bill Clinton, who was deeply unpopular in South Carolina following his impeachment on allegations of lying about his marital infidelity.

Bush defeated McCain in South Carolina and then finished him off on Super Tuesday in March. McCain withdrew and eventually endorsed Bush, but their relationship would take years to recover. He returned to the Senate as independent as ever, voting with Democrats against Bush’s signature tax cuts and working with Feingold to enact campaign-finance reform. McCain’s isolation from Bush and the Republican leadership reached the point where Democrats tried to recruit him to switch parties or leave the GOP. In 2004, Senator John Kerry, a fellow Vietnam veteran and the Democratic presidential nominee, approached McCain about joining his ticket. But McCain later said he never gave the idea serious consideration and endorsed Bush’s reelection bid.

By 2008, it was McCain’s turn to lead a Republican Party that, to that point, tended to nominate the next in line, the experienced over the fresh-faced. But McCain’s decision to run as a front-runner backfired. He modeled his campaign on Bush’s in 2000, but in the face of conservative resistance and poor management it had collapsed under its own weight by the fall of 2007. The near-death experience may have been a blessing in disguise. Embracing his status as an unlikely underdog, McCain launched his comeback with a win, again, in New Hampshire. He had regained the momentum, and with a front-loaded primary calendar, he became the presumptive nominee by the spring.

McCain’s handling of the general-election campaign would open fissures in the Republican Party that exploded in the decade to come. Running against a man seeking to make history as the first black president, McCain did not indulge the smear campaign that was already rampant on the right, suggesting Obama was neither a Christian nor an American citizen. “No, ma’am,” he memorably told a woman who claimed Obama was “an Arab” at a campaign event. “He’s a decent family man, citizen, who I just happen to have disagreements with on fundamental issues.”

Leaning on his military service and maverick reputation in the Senate, McCain ran under the banner of “Country First”—a strategic choice aimed in part on countering a damaged GOP brand weighed down by an unpopular president. But the decision that came to define his campaign would be his choice of Sarah Palin as his running mate. The move initially excited conservatives who rallied behind the folksy, and feisty, Alaska governor. But her inexperience and her performance in media interviews left many voters with the impression she would be unprepared for the presidency, a concern compounded by McCain’s age and cancer history.

McCain’s 2008 fate was probably sealed in September, when the financial markets began to crash and he abruptly “suspended” his campaign to return to Washington. His response to the crisis was panned as rash—David Letterman mocked him for canceling an appearance on his show—and undermined his argument that in contrast to the younger Obama, his was the steadier hand to lead the nation during a period of economic turbulence. On November 4, 2008, Obama decisively defeated McCain to win the presidency by more than seven points, carrying 365 electoral votes to McCain’s 173.

The Palin pick would follow McCain for the final decade of his life. His critics—and even some of his allies—saw the decision as impulsive and possibly desperate, undermining McCain’s emphasis on experience and national security. To their dismay, its impact would endure far beyond a losing presidential campaign. McCain’s decision to elevate Palin helped unleash the conservative uprising that would become the Tea Party, and though her political career would fade, the marriage of populism and nativism that Palin embraced during the Obama administration became the main themes driving Donald Trump’s winning campaign in 2016.

McCain defended the Palin choice for years, and she returned the favor by not turning on him even as she embraced the kind of hard-edged conservative politics—and politicians—that he came to loathe. But shortly before his death, McCain did voice regret about the pick in a final memoir and in an HBO documentary, saying he wished he had ignored his advisers and tapped his friend Senator Joseph Lieberman, the former Democratic vice-presidential nominee who had become an independent and endorsed McCain.

In the Senate, McCain sparred with Obama just as he had done with Bush. He locked arms with Republicans on opposing most of the new president’s domestic agenda, including an economic-stimulus package in 2009 and the Affordable Care Act the following year. A hawk in foreign affairs, he criticized the Democrat’s drawdown of troops in Iraq and his handling of crises in Libya and Syria. McCain also veered back to the right as he faced a primary challenge during the Tea Party wave of 2010. He backed off his support for comprehensive immigration reform and emphasized border security, despite having worked with the late Senator Ted Kennedy of Massachusetts on proposals that would have offered a path to citizenship for undocumented immigrants. After winning reelection, however, he returned to his previous stance and co-sponsored similar legislation that passed the Senate but not the House.

By Obama’s second term, McCain’s presidential ambitions were gone, and he embraced the role of an elder statesman in the Republican Party. Over the years, his enduring popularity with the public was in no small part due to his relationship with the press. Whether in his regular appearances on the Sunday talk shows, his frequent scrums in the hallways of the Senate, or his kibitzing on the “Straight Talk Express” during his campaigns, McCain flattered reporters with access, candor, and humor. In speeches, he’d often refer to the press as “my base.” It was a joke that rang true.

McCain would have one more president to do battle with, but his bickering with Trump began nearly a year and a half before the nation’s 45th president took office. “He’s not a war hero,” Trump said of McCain at a campaign event in Iowa in 2015. “He was a war hero because he was captured. I like people who weren’t captured.”

The remark drew widespread condemnation from Republicans and Democrats alike, who rose to McCain’s defense. And it would linger as a backdrop to a relationship that became even more frayed during Trump’s first year. In July 2017, as Senate Republicans were preparing to consider legislation to repeal the ACA and fulfill a central campaign promise, McCain was diagnosed with an aggressive form of brain cancer—the same kind that had killed his friend Ted Kennedy eight years earlier. Republicans pushed back a key vote to wait for McCain to return, and when he did, he helped them bring the repeal bill to the floor. But he delivered a stem-winder of a speech that was part valedictory, part excoriation of the decaying of Senate norms, and part warning to Trump. “Whether or not we are of the same party, we are not the president's subordinates. We are his equal!” McCain declared.

Two days later, a new version of the repeal bill came up for a climactic vote. A pair of Republicans had already announced their intention to vote no, but if every other GOP senator voted yes, the measure would pass. It was well past midnight, and it came down to McCain. He strode into the chamber, approached the well, and signaled to the clerk. With a demonstrative flick of his wrist, he gave a thumbs down, and the amendment failed. At the age of 80 and months from death, McCain had turned against his party and, whether by design or not, preserved the domestic legacy of the man who had kept him from the presidency nine years earlier. He had played the maverick one last time.

We want to hear what you think about this article. Submit a letter to the editor or write to letters@theatlantic.com.