Python slicing a string using space characters and a maximum length

Posted by chrism on Stack Overflow See other posts from Stack Overflow or by chrism
Published on 2010-04-12T13:58:15Z Indexed on 2010/04/12 14:23 UTC
Read the original article Hit count: 400

Filed under:
|
|

I'd like to slice a string up in a similar way to .split() (so resulting in a list) but in a more intelligent way: I'd like it to split it into chunks that are up to 15 characters, but are not split mid word so:

string = 'A string with words'

[splitting process takes place]

list = ('A string with','words')

The string in this example is split between 'with' and 'words' because that's the last place you can split it and the first bit be 15 characters or less.

© Stack Overflow or respective owner

Related posts about python

Related posts about string