Hillary Clinton and Bernie Sanders, now in the two-week stretch before the first votes come, are laying down a choice for Democrats: Lead with their heads, or with their hearts.

As the race has gotten tighter than just about anyone would have expected a few months ago, the two leading contenders for the Democratic presidential nomination are engaging each other more intensely — and drawing distinctions that could reshape the identity of their party.

History suggests that it is difficult for any party to keep the White House for three terms. For Democrats, the question is whether the best path to retaining power is the pragmatic or the ideal.

“This campaign is about a political revolution to not only elect the president but to transform this country,” Sanders, a senator from Vermont who calls himself a democratic socialist, said during their debate Sunday night in Charleston, S.C.

The Democratic battle is not the bitter rebellion of the grass roots against the establishment that is underway on the Republican side. For Democrats, 2016 is turning into a soul-searching exercise, reminiscent of many contests the party has seen in recent decades.

A Bernie Sanders supporter carries a sign through a maze of political sign for Sanders and opponent Hillary Clinton before the start of the debate. (Randall Hill/Reuters)

After a week when the former secretary of state had been thrown off balance by a set of alarming poll numbers, she was back on surer footing during Sunday’s debate, presenting herself as the pragmatic pick — a reliable steward of President Obama’s achievements and a realist with the experience to pick the fights that are winnable in an era of polarized politics.

“As we’re coming into the close here, the question is who can go toe to toe with the Republicans and protect the progress made under President Obama,” Robby Mook, Clinton’s campaign manager, told reporters after the debate.

Clinton’s aura of inevitability could be dented if she loses the Feb. 1 Iowa caucuses, reminding voters of how she stumbled the first time she ran as a formidable front-runner in 2008.

For the first time, having Sanders as their standard-bearer is no longer outside the realm of possibility for Democratic elders.

“I’m concerned that candidates in purple states would really face some problems with Senator Sanders at the top of the ticket,” Delaware Gov. Jack Markell said in an interview.

“There has been a lot of energy in the Warren-Sanders-de Blasio wing of the party,” Markell added, referring to Sen. Elizabeth Warren of Massachusetts and New York Mayor Bill de Blasio. “I think that is unfortunate, both in terms of substance and in terms of politics. . . . We’ve got to have a message that’s compelling not only to Democrats but also to independents and even a few Republicans.”

[The fourth Democratic debate, annotated: Who said what and what it meant]

Supporters of Sanders and Clinton rallied outside the Gaillard Center before the start of the debate. (Scott Olson/Getty Images)

Sanders, who, according to polls, has closed the gap with Clinton in Iowa and is leading her in New Hampshire, has struck a chord with the Democratic base by invoking both their aspirations and their disappointments that the Obama years have not delivered what they hoped. He also argues that Democrats — including Clinton — have been too cozy with Wall Street and the big banks.

“People are tired of being dominated by big-money interests, and Bernie Sanders expresses that, and I think he expresses it in a way that says, ‘You know what? As Democrats, we also need to be about bold ideas, and not be afraid of bold ideas,’ ” said Rep. Donna F. Edwards (D), who is running for a U.S. Senate seat in Maryland.

One of the boldest of those is a single-payer health-care system, which Sanders describes as Medicare for all — a massive expansion of government that many liberals believe is the only way to reach their cherished goal of medical coverage for everyone.

That was the area where the two candidates had some of their harshest clashes during Sunday’s debate, their final one before the Iowa caucuses.

“The fact is, we have the Affordable Care Act. That is one of the greatest accomplishments of President Obama, of the Democratic Party and of our country,” Clinton argued. “Now, there are things we can do to improve it, but to tear it up and start over again, pushing our country back into that kind of a contentious debate, I think is the wrong direction.”

“No one is tearing this up. We’re going to go forward,” Sanders retorted. “But [what] the secretary neglected to mention, not just that 29 million still have no health insurance, that even more are underinsured with huge co-payments and deductibles. . . . The vision from FDR and Harry Truman was health care for all people, as a right, in a cost-effective way.”

There are those in Clinton’s camp who still dismiss the idea that Sanders is a threat to her, even if he wins Iowa and New Hampshire.

As the race heads south after that, Clinton is counting on her more centrist profile, and her long-standing ties to African American voters, to carry her through. In South Carolina, for instance, black voters constituted 56 percent of the Democratic primary electorate in 2008 and could account for an even greater share this year, said Jamie Harrison, the state party chairman.

“I never, ever expected that Senator Bernie Sanders would get this far. But really, I don’t see how he is any particular political challenge,” said Rep. Charles B. Rangel (D-N.Y.), a founding member of the Congressional Black Caucus.

“No one knows who the hell Bernie Sanders is, and it’s not his fault,” he added. “Bernie’s a nice guy from Brooklyn, found his way to Vermont and the House and the Senate, but it has nothing to do with establishing relationships with minority communities.”

Others, however, say that beating the formidable Clinton operation in the first two contests could change the dynamic of the entire race.

“Winning cures many ills in a campaign,” said Jeff Weaver, Sanders’s campaign manager. “People give you a second look, or a first look in some cases.”

That was what happened in 2008, when Obama, a first-term senator, pulled out a stunning victory in Iowa.

“I think if Bernie wins Iowa and New Hampshire, I would bet, not the farm, but I would bet he wins South Carolina,” said Phil Noble, a Charleston businessman who was one of Obama’s earliest supporters in the state and who is now backing former Maryland governor Martin O’Malley.

“The ‘safe’ candidate is Hillary. To move to Bernie, it’s almost as though we need permission,” ­Noble added. “That’s what Iowa did for Obama.”

[Democratic debate: A choice between continuity and change]

Jacqueline Gowe, a 20-year resident of Charleston’s Daniel Island, is one who has been feeling the pressure to change her allegiance from Clinton to Sanders. It is coming from her 17-year-old son, Christopher. The two worked as volunteers in the cloakroom at Sunday’s debate.

“My son has been relentless in talking to me and trying to persuade me. He believes that Hillary is an establishment candidate, and he really drove this until I’m beginning to see things differently,” Gowe said.

She respects Clinton and would like to see a female president, Gowe said. But with Christopher and his two siblings not that far from going to college, she also finds Sanders’s promise of free tuition at public universities appealing.

Many Clinton allies say they were worried last week when her campaign began lashing out at Sanders — in some cases, mischaracterizing his positions.

Her daughter, Chelsea, for instance, claimed during an appearance in New Hampshire that the senator from Vermont “wants to dismantle Obamacare, dismantle the [children’s health insurance] program, dismantle Medicare and dismantle private insurance.”

Some Democrats noted that, skilled and seasoned as Hillary Clinton is at deflecting Republican criticism, she is not so deft when the challenge comes from within her own party.

“She doesn’t take incoming fire well. She hunkers down,” one longtime Clinton adviser said. “She gets very defensive and lashes out.”

Indeed, as her campaign stepped up its attacks on her rival’s health-care plan last week, his campaign circulated a 2008 video of an outraged Clinton complaining that Obama was doing the same to her.

“Since when do Democrats attack one another on universal health care?” Clinton said. “So shame on you, Barack Obama.”

This is far from the first time that Democrats have seen this type of struggle within their party between pragmatism and idealism.

Usually, the establishment-backed front-runner wins — as President Jimmy Carter did over Sen. Edward M. Kennedy (Mass.) in 1980, former vice president Walter Mondale did over Sen. Gary Hart (Colo.) in 1984, and Vice President Al Gore did over Sen. Bill Bradley (N.J.) in 2000.

And occasionally, the insurgent triumphs — as Sen. George McGovern (S.D.) did over Sen. Edmund Muskie (Maine) in 1972 and, of course, Obama did over Clinton in 2008.

But it nearly always takes a toll. Of all of those races, the only time the Democratic nominee prevailed in a general election was when Obama won.

In the end, many believe that liberals will find their way to embracing Clinton, especially if, as expected, she ends up being their only alternative to whomever the Republicans nominate.

“When you cut through it, progressives can be comfortable with her running at the top of the ticket,” said Steve Rosenthal, a longtime political strategist for the labor movement. “But it’s going to take a while to get them there.”

Abby Phillip, Philip Rucker, John Wagner and Vanessa Williams contributed to this report.