chopping a string

Peter Hansen peter at engcorp.com
Mon Mar 4 21:07:19 EST 2002


David Bear wrote:
> 
> there's gotta be an easier way.  I have a string
> "    something  somethingelse    "
> 
> 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])
> 
> but I'm thinking, there must be a better way?  better means (faster) (smaller)..

Did you try this?

>>> a = '  test  this   '
>>> a.split()
['test', 'this']
>>> a.split()[0]
'test'



More information about the Python-list mailing list