95

How can I strip the comma from a Python string such as Foo, bar? I tried 'Foo, bar'.strip(','), but it didn't work.

msampaio
  • 3,394
  • 6
  • 33
  • 53

4 Answers4

176

You want to replace it, not strip it:

s = s.replace(',', '')
eumiro
  • 207,213
  • 34
  • 299
  • 261
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
  • 2
    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... – Jayme Tosi Neto Jun 21 '17 at 11:06
7

unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))

maow
  • 1,387
  • 11
  • 20
1

This will strip all commas from the text and left justify it.

for row in inputfile:
    place = row['your_row_number_here'].strip(', ')

‎ ‎‎‎‎‎ ‎‎‎‎‎‎

Moe Rin
  • 15
  • 1
  • 5
Shal
  • 11
  • 1