I have a long string like:
mystring = 'It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...'''
And I'd like to insert a linebreak every 20 letters. Except I don't want to split a word in half, so if it splits at 22 letters, that's fine. Does anyone know of an intelligent solution to this?
Use textwrap :
>>> mystring = 'It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...'''
>>> import textwrap
>>> textwrap.wrap(mystring, width=20)
['It was the best of', 'times, it was the', 'worst of times, it', 'was the age of', 'wisdom, it was the', 'age of', 'foolishness...']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.