Search code examples
pythonpython-3.xtextsplittokenize

Split string every n characters but without splitting a word


Let's suppose that I have this in python:

orig_string = 'I am a string in python'

and if we suppose that I want to split this string every 10 characters but without splitting a word then I want to have this:

strings = ['I am a ', 'string in ', 'python']

or this (without the whitespaces at the splittings):

strings = ['I am a', 'string in', 'python']

Therefore, the split should be done exactly before the word which would have been splitted otherwise at each case.

Otherwise, I would have this:

false_strings = ['I am a str', 'ing in pyt', 'hon']

Just to mention that in my case I want to do this every 15k characters but I gave the example above for every 10 characters so that it could be written in a concise way here.

What is the most efficient way to do this?


Solution

  • You can use built-in textwrap.wrap function (doc):

    orig_string = 'I am a string in python'
    
    from textwrap import wrap
    
    print(wrap(orig_string, 10))
    

    Prints:

    ['I am a', 'string in', 'python']