How to strip comma in Python string

Question:

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 By: msampaio

||

Answers:

You want to replace it, not strip it:

s = s.replace(',', '')
Answered By: eumiro

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
Answered By: pradyunsg

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

Answered By: maow

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

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


‎‎‎‎‎
‎‎‎‎‎‎

Answered By: Shal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.