chopping a string
Carel Fellinger
cfelling at iae.nl
Mon Mar 4 23:28:08 EST 2002
David Bear <iddwb at moroni.pp.asu.edu> wrote:
> there's gotta be an easier way. I have a string
>
> note the leading a trailing whitespace. I'd like to grab the first
> word and strip whitespace. I came up with
> string.join(string.split(string.strip(str))[:1])
This looks very complicated, maybe you just ment:
>>> string.split(" something somethingelse ", maxsplit=1)[0]
'something'
To get the same effect with string methods be ware that you'll have to
specify None as seperator to get the special space treatment:
>>> " something somethingelse ".split(None, maxsplit=1)[0]
'something'
The maxsplit=1 is there to speed things up, it stops after the first split.
--
groetjes, carel
More information about the Python-list
mailing list