How to replace a comma

Hendrik van Rooyen mail at microcorp.co.za
Tue Dec 19 00:02:22 EST 2006


"Lad" <python at hope.cz> top posted:

<top posting fixed>

> > re's are a pain.  Do this instead:
> >
> > >>> s = "hello, goodbye,boo"
> > >>> s.replace(', ',',')
> > 'hello,goodbye,boo'
> > >>> _.replace(',',', ')
> > 'hello, goodbye, boo'

> Thank you for ALL for help.
> Hendrik,
> your solution works great but
> what is `_`  in
> _.replace(',',', ')
> 
> for?

The underscore, in the interactive interpreter, stands for
the result of the last statement.  So in the above, you can 
think of it as the "name" of the string without spaces after 
the commas, on the previous line.

Note that to fix pathological stuff like:

s = "asasd, oipuopioiu,       this is bad shit,\tiuiuiu,,, , "

you will have to do more work than the simple solution
above...

I am constantly amazed by how different we all are, when
I read the diverse solutions to such a seemingly simple
problem - and the nice thing is that they all work...

Have a good look at Peter Otten's solution - try to 
understand it - it uses most of the very useful Python
functions.


- Hendrik




More information about the Python-list mailing list