Truncate a string without ending in the middle of a word

后端 未结 8 1820
无人共我
无人共我 2020-12-12 17:17

I am looking for a way to truncate a string in Python that will not cut off the string in the middle of a word.

For example:

Original:          \"This is          


        
8条回答
  •  遥遥无期
    2020-12-12 18:00

    >>> import textwrap
    >>> textwrap.wrap('The quick brown fox jumps over the lazy dog', 12)
    ['The quick', 'brown fox', 'jumps over', 'the lazy dog']
    

    You just take the first element of that and you're done...

提交回复
热议问题