chopping a string

Trent Mick trentm at ActiveState.com
Mon Mar 4 23:32:45 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)..

    >>> s = "    something  somethingelse    "
    >>> s.split()[0]
    'something'

If you are using Python 1.5.2, then you spell this this way:

    >>> import string
    >>> string.split(s)[0]
    'something'
    >>>


Cheers,
Trent


-- 
Trent Mick
TrentM at ActiveState.com




More information about the Python-list mailing list