I have a string like this:
Trump, Defending Himself After Flynn Guilty Plea, Says FBI Is in 'Tatters' | CVS to Buy Aetna for $69 Billion in a Deal that May Reshape the Health Industry | Joy Reid Apologizes for Past Anti-Gay Articles: 'Insensitive, Tone Deaf and Dumb' | California 18-year-old confesses to molesting dozens of children | Bill Belichick Apologizes for Rob Gronkowski's Late Hit, Calls It 'Bulls--t' | Met Opera Suspends James Levine After New Sexual Abuse Accusations | Like it or not, Alabama brings legitimacy to this year's College Football Playoff | Trump's campaign: Big Macs, screaming fits and constant rivalries | Manhattan equity director mauled to death by shark while scuba diving off Costa Rican coast | Man Stabs Two in Queens, Then Drives Into Their Helpers, Police Say | Here's how the Rangers might be able to separate themselves from other contenders for Shohei Ohtani | Alabama's Disdain for Democrats Looms Over Its Senate Race | Billy Bush confirms it was Trump's voice on 'Access Hollywood' tape: 'Yes, Donald Trump, you said that' | Andy Reid: Darrelle Revis didn't play in second half because he played a lot in first half | Geno Smith calls out 'coward' Rex Ryan: 'I saved his job' | Jimmy Garoppolo gives the 49ers exactly what they need, plus more Week 13 notes | Enter the 'Petro': Venezuela to Launch Oil-Backed Cryptocurrency | Wiring blamed in failed Pontiac Silverdome implosion | McConnell predicts unpopular tax bill will be a winning issue for GOP | Broncos drop eighth straight in ugly loss to Dolphins |
It's a list of news headlines parsed from the Google News RSS Feed. I'm sending the data over serial to a LCD which has 2 lines with 16 characters each. Currently, I split the string into 32 character parts, then display each part for a fixed length of time. The problem with this is that it displays only part of the last word in most cases, and in some cases, only part of the first word, depending on how the string is split. So, how can I split the string by the whitespace, to prevent splitting the words, and still try to be less than the 32 character limit.
An example using the above text would be:
First line: Trump, Defending Himself After
Second line: Flynn Guilty Plea, Says FBI Is
and so and so on.
You can create own definition and define limit as follows and you can iterate over list.
str = "Trump, Defending Himself After Flynn Guilty Plea, Says FBI Is in 'Tatters'"
def split_string(str, limit, sep=" "):
words = str.split()
if max(map(len, words)) > limit:
raise ValueError("limit is too small")
res, part, others = [], words[0], words[1:]
for word in others:
if len(sep)+len(word) > limit-len(part):
res.append(part)
part = word
else:
part += sep+word
if part:
res.append(part)
return res
print split_string(str=str, limit=32)
output:
['Trump, Defending Himself After', 'Flynn Guilty Plea, Says FBI Is', "in 'Tatters'"]
You are trying to wrap the text to a 32-character line length. The textwrap module in the standard library does this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With