John McCain was the Vietnam war hero who became one of America's most high-profile politicians.
His brand of common-sense conservatism often saw him clash with his Republican allies and he was a fierce critic of the presidency of Donald Trump.
He gained a reputation for being a dealmaker, someone who was prepared to compromise to get the job done.
But he failed to win the 2008 presidential election, facing criticism from some quarters for his choice of Alaska governor Sarah Palin as his running mate.
John Sidney McCain III was born on 29 Aug 1936 at Coco Solo Naval Air Station in the Panama Canal Zone, an area then under US control. Naval service was in his blood, both his father and grandfather ending their naval careers as four star admirals.
His father's various postings saw the young McCain attending a host of different schools before he enrolled in the United States Naval Academy at Annapolis, Maryland.
There he established a reputation for leadership, coupled with a fine disdain for authority. The latter trait, along with his propensity to do just enough to achieve grades in subjects he found boring, saw him graduate in 1958 near the bottom of his class.
He moved on to Pensacola in Florida, dubbed the cradle of naval aviation where, after a two-and-a-half year course, he left with his pilot's qualifications and a reputation as a party animal.
He wasn't the most competent of flyers. While still under training he crashed a plane into the sea, leading him to be dubbed "cocky, occasionally cavalier and prone to testing limits".
"John was what they call a push-the-envelope guy," was the verdict of one of the pilots who flew with him.
After being assigned to fly Skyraider aircraft from a US carrier in the Mediterranean he flew his plane into electrical wires in Spain, causing a power failure in the surrounding area.
In 1967 he was transferred to the US carrier Forrestal which was engaged in the operation dubbed "Rolling Thunder" - the sustained bombing of North Vietnam. Following a fire on the Forrestal, in which he was injured, he transferred to the USS Oriskany.
On 26 Oct 1967 he was shot down while flying a mission over Hanoi. Badly injured - having fractured both arms and a leg when he ejected - he was captured by North Vietnamese troops.
He was refused all but the most rudimentary medical care, and interrogated under torture. The beatings only stopped when the North Vietnamese discovered he was the son of an admiral. In March 1968, he was placed in solitary confinement where he remained for two years.
When his father became commander of US forces in Vietnam, his captors - sensing a propaganda opportunity - offered to release him. McCain refused unless every US prisoner captured before him was released first.
His captors restarted the brutal torture regime to the extent that McCain, beaten and suffering from dysentery, attempted suicide. He was eventually released after five and half years in captivity, but with lasting injuries.
He returned to find his wife Carol, whom he had married in 1965, had been badly injured in a car accident which had left her disfigured.
A spell at the Navy's liaison office to the US Senate in 1977 kindled an interest in politics. He also began dating a teacher, Cindy Lou Hensley, whom he married in 1980 after his first wife had granted him a divorce.
In 1981 he quit the Navy and set out to run for Congress, winning a seat in Phoenix, Arizona, where his new father-in-law ran a beer distribution company.
He was a strong supporter of the then-president, Ronald Reagan, including favouring a hard line against the Soviet Union and the backing of the Contras in Nicaragua.
Re-elected to Congress with an overwhelming majority in 1984, he was asked to serve on the influential House Foreign Affairs Committee, which further raised his profile.
In 1987, he entered the Senate to represent Arizona, replacing the arch-conservative Barry Goldwater who had retired. His expertise in the Navy led to a seat on the Senate Armed Forces Committee.
He also served on the Indian Affairs Committee where, as a keen gambler himself, he pushed through new laws that allowed native Americans to generate revenue from casinos.
McCain had already established a reputation of not being afraid to challenge his own party leadership. He caused a stir when he twice supported President Clinton's nominees for the Supreme Court, judging them to be the best people for the job.
In the early 90s, the new senator was involved in a corruption scandal after he and four Democratic senators were accused of trying to intimidate regulators on behalf of a campaign donor who eventually served prison time for corrupt management practices.
He received a mild warning after an investigation found he "exercised poor judgment in intervening with the regulators" but that his actions "were not improper".
McCain announced he was running for the Republican nomination for president in September 1999.
He pitched himself as the anti-establishment candidate against fellow candidate, Texas Governor George W Bush, who could rely on most of the support - and the money - from the party hierarchy.
In what became a bitter campaign, Bush proved adept at harnessing the support of the Christian right and McCain, after a heavy defeat on so-called Super Tuesday, quit the race in March 2000.
While he pledged his support for Bush, McCain characteristically showed he was not afraid to criticise the new president. He was one of only two Republican senators to vote against tax cuts and also opposed the president's policies on gun control.
However, after the 9/11 attacks he supported Bush's war in Afghanistan and the invasion of Iraq, although as the latter operation dragged on, he began to question the Bush strategy and the policies of the US defence secretary, Donald Rumsfeld.
In April 2007, McCain announced he would again run for president and fought off a strong challenge from New York mayor Rudy Giuliani and the Massachusetts governor Mitt Romney for the Republican nomination.
Mindful of his need to attract support from conservative Christians, he surprised everyone by announcing virtually unknown Sarah Palin, the governor of Alaska, as his running mate.
But while Palin's hockey mum persona was welcomed by the right, mainstream voters began to question her lack of experience and a series of poor performances in TV interviews cast doubts on the credibility of the Republican campaign.
Outspent by the Obama campaign, and carrying the legacy of eight years of a Bush presidency, McCain lost both the electoral college and the popular vote and Obama entered the White House.
Unusually, the relationship between the two men seemed to blossom after the election and Obama often consulted McCain. However, the senator came out strongly in opposition to the new president's plans to stimulate the economy, claiming it was money spent to no useful purpose.
Back in Arizona, he fought off a challenge by a Tea Party candidate and easily retained his Senate seat in the 2010 elections. His profile in the Senate remained high and his regular appearances on US TV maintained his position as one of the best known politicians in the US.
When Republicans began selecting their presidential candidate for the 2016 election, McCain expressed grave doubts over Donald Trump's suitability for the job, once referring to a roomful of Trump supporters as "crazies".
The relationship between the two men took a further knock when Trump criticised McCain's war record.
"He's not a war hero," Trump opined. "He's a war hero because he was captured? I like people who weren't captured."
McCain withdrew his support for Trump in October 2016, the day after a 2005 recording emerged of Trump making obscene remarks about women and appearing to admit to sexual assault.
"When Mr Trump attacks women and demeans the women in our nation and in our society, that is a point where I just have to part company," he said.
However, Trump, along with Barack Obama, was among the first to offer good wishes to McCain when, in July 2017, it was announced that he was suffering with a brain tumour.
The same month - while sporting a scar over his left eye after surgery for a blood clot - he dramatically used a "thumbs down" to vote against a Republican health care repeal bill.
The president tweeted that McCain, and other senators who voted against the bill, had "let the American people down".
John McCain's determination to plot his own political course, regardless of the party line, probably explains why he never reached the highest office in US politics.
But he was seen as a man of principle, someone happy to sit down with his political opponents if there was a compromise to be reached.
"I will work with anyone to get this country moving again," he once said. "I will listen to any idea that is offered in good faith and intended to help solve our problems."