How can I strip the comma from a Python string such as Foo, bar
? I tried 'Foo, bar'.strip(',')
, but it didn't work.
Asked
Active
Viewed 2.1e+01k times
4 Answers
20
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

pradyunsg
- 18,287
- 11
- 43
- 96
-
2Man! 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... – Jayme Tosi Neto Jun 21 '17 at 11:06