How to split string

Johny python at hope.cz
Wed Dec 5 06:46:27 EST 2007


On Dec 5, 11:55 am, Chris <cwi... at gmail.com> wrote:
> On Dec 5, 10:55 am, Johny <pyt... at hope.cz> 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?
> > Thank you
> > L.
>
> That's not a great example of what you are looking for, because that
> result just looks like:
> '\n'.join(S.split())
>
> What result would you be looking for with say:
> S='this is just a random sequence of letters courtesy of monkeys on
> typewriter.'

Chris,
Thank you for your reply.
So, let's suppose the  example:
S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'
and the length of split strings is now 10( instead of 6)
The example should be like this
this is
just a
random
sequence
of letters
courtesy
of monkeys
on
typewriter


In other words, the string should be split at every 10th possition but
if the 10th character is space, then the string must be split at the
nearest space before the 10th possition.

It could be better explained if the length of split strings will be
20.

S='this is just a random sequence of letters courtesy of monkeys on
> typewriter.'

Results:

this is just a
random sequence of
letters courtesy of
monkeys on
typewriter.'


Any idea how to do that?
Thank you
L.







More information about the Python-list mailing list