How to split string

Peter Otten __peter__ at web.de
Wed Dec 5 06:25:07 EST 2007


Johny wrote:

> I have a string of a variable length and I need to split the string
> in strings of 6 characters .
> But if the 6th character is not space then I must split the string
> at possition  before the 6th character.
> 
> For example:
> if the main string  S is
> 
> S='abcde fghc ijkl mnop'
> 
> the result should be
> abcde
> fghc
> ijkl
> mnop
> 
> 
> Do you have any idea how it can be done?

>>> print textwrap.fill('abcde fghc ijkl mnop verylongword', width=6)
abcde
fghc
ijkl
mnop v
erylon
gword
 
Peter



More information about the Python-list mailing list