removing duplicate spaces from a string

Alex Martelli aleaxit at yahoo.com
Tue Aug 7 18:07:37 CEST 2001


"Rich Harkins" <rharkins at thinkronize.com> wrote in message
news:mailman.997130350.25505.python-list at python.org...
> Since the default action of split is to seperate by white space (any
number
> or type of white space will do), the following one liner would do:
>
> >>> s='a  b c  d'
> >>> ' '.join(s.split())
> 'a b c d'

...as long as you're real *SURE* that s doesn't contain
OTHER forms of whitespace, such as tabs or newlines, or
you're OK with them mutating into spaces...


Alex






More information about the Python-list mailing list