How can I strip the comma from a Python string such as Foo, bar? I tried 'Foo, bar'.strip(','), but it didn't work.
4 Answers
Use replace method of strings not strip:
s = s.replace(',','')
An example:
>>> s = 'Foo, bar'
>>> s.replace(',',' ')
'Foo bar'
>>> s.replace(',','')
'Foo bar'
>>> s.strip(',') # clears the ','s at the start and end of the string which there are none
'Foo, bar'
>>> s.strip(',') == s
True
1 Comment
Jayme Tosi Neto
Man! It's so obvious! SO OBVIOUS! But so obvious that I was using strip and trying to understand why it didn't work as a bulk replace...
unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))
1 Comment
alphazwest
+2 for being a one-liner in addition to overall ridiculousness