removing duplicate spaces from a string
Markus Schaber
markus at schabi.de
Tue Aug 7 14:03:49 EDT 2001
Hi,
Rajarshi Guha <rajarshi at seed.chem.psu.edu> schrub:
> say the string is s, then you could proceed as follows:
>
> o = ''
> s = string.split(s,' ')
> for i in s:
> if (i != ' '):
> o = o + i + ' '
>
> This code segment will give you the final required ouput in o
But it is bad :-)
For every loop iteration, it produces two new string objects. And all
except the last are just food for the garbage collection.
Join is just the better way.
markus
--
Defend democrathy and freedom!
More information about the Python-list
mailing list