CEDAR RAPIDS, Iowa — Pete Buttigieg ran through his stump speech (six minutes on generational change), tossed bean bags with activists (in front of as many cameras as Iowa voters), took seven questions from reporters ("How does it feel to be a rock star?’’) and pounded out some blues on an electric keyboard (a Miles Davis tune).

If Buttigieg didn’t spend much time talking to voters at his campaign picnic last weekend, he did stick to his winning formula: doing everything possible to reach bigger audiences on their screens.

More than most of his Democratic rivals, Buttigieg, the mayor of South Bend, Indiana, has cracked the code of the early months of the presidential campaign, embracing TV appearances while mastering the art of creating moments for social media and cable news. The 37-year-old’s campaign was the first to grasp that the early primary race would unfold on mobile devices and televisions instead of at the traditional town-hall gatherings and living rooms in the early states.

He’s not alone: Sen. Elizabeth Warren of Massachusetts has inundated reporters with policy proposals, prompting hours of cable news coverage and forcing fellow candidates to respond to her ideas during live interviews.

Over the first six months of the presidential campaign, Buttigieg and Warren have outmaneuvered the other 21 Democratic candidates, demonstrating an innate understanding of the value of viral moments and nonstop exposure that drives politics in the Trump era.

Get the news delivered to your inbox: Sign up for our politics newsletter

Buttigieg, a man who has made himself all but unavoidable for comment, rode a wave of positive press about his personal story: a Harvard-educated Rhodes scholar who, during his two terms as mayor, has served in Afghanistan as a Naval intelligence officer, come out as gay and gotten married. In doing so, he reached the first tier of Democratic presidential candidates.

Warren took the opposite route to the same destination. Rather than lean into her biography, she rolled out unusually detailed domestic policy plans to grab headlines and inspire activists. She also earned attention for her devotion to taking photographs with every attendee at her events who wants one — more than 30,000 to date, she said last week.

The two have seen their strategies pay dividends. Each vaulted to the top of a major poll of Iowa Democrats released last weekend by the Des Moines Register and CNN, which placed Warren and Buttigieg in a virtual tie for second place with Sen. Bernie Sanders of Vermont, behind former Vice President Joe Biden. And Wednesday, Warren surpassed Sanders in a poll of likely Democratic caucus-goers in Nevada, another key early voting state.

Unlike many of their rivals, who built their political careers in the era of carefully chosen interaction, the two have placed their fate in the hands of TV bookers and online viral content.

Erik Smith, a Democratic strategist who worked on Barack Obama’s presidential campaigns, said none of the candidates has experience running in such a crowded primary contest. ‘‘They’re used to having a two-way primary and a two-way general,’’ he said. ‘‘Those habits don’t serve you well in a multi-candidate primary, particularly one as long and substantive as this.’’

Buttigieg and Warren began their rise in the public polling as they became more frequent presences on cable TV. Since April 1, the most-mentioned Democratic presidential candidates on CNN, Fox News and MSNBC are Biden, Sanders, Warren and Buttigieg, according to data from the Internet Archive’s Television News Archive.

Buttigieg, who rose from 1% support to 14% in three months in polling conducted for the Des Moines Register and CNN, has been powered largely by appearances on televised town hall-style programs, which have helped him create a fundraising colossus rivaled only by Biden’s when it comes to tapping major donors, said Rufus Gifford, finance chairman for Obama’s re-election campaign.

‘‘It’s Trumpian, to a certain extent, in that it’s refreshing in its honesty,’’ said Gifford, who has co-hosted fundraising events for both Biden and Buttigieg. ‘‘There’s something about him where he feels less polished than the other candidates in the race, in a good way.’’

Darcie Derby, a 43-year-old who works at a Cedar Rapids logistics company, said she first saw Buttigieg during his February appearance on Stephen Colbert’s talk show. Since then she and her sister have been sharing Buttigieg stories and memes on their Facebook pages, she said.

Warren also has used a nonstop media push to separate herself from the second tier of candidates.

She rolled out her plan to break up big tech companies in New York at a moment when anger at Amazon for planning an expansion in Queens was near its peak. Later she introduced an opioids proposal during a trip to West Virginia.

Warren attributed her recent polling rise — from 9% to 15% in the Register’s surveys — to her ability to make Democrats aware of what she would do as president. At a recent gathering in Cedar Rapids, Warren’s staffers chanted, ‘‘I’m a Warren Democrat,’’ an implicit assertion that she has created her own lane in the crowded field.

‘‘It’s about having ideas,’’ Warren said. ‘‘About being able to say specifically, ‘Here’s what’s gone wrong in this country, how corruption has put us on the wrong path,’ and then having very specific plans to go after it.’’

Asked whether it was difficult to attract attention in the crowded field, Warren offered a one-word response: ‘‘No.’’

In April and May, Warren and Buttigieg outspent most of the Democratic field in advertisements on Facebook and Google. Warren spent $1.1 million and Buttigieg $975,700, according to tracking from Bully Pulpit Interactive, a Democratic political firm. Only Biden, who spent $1.5 million after his late-April campaign launch, and Kamala Harris, whose campaign spent $1 million, are close.