98

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

0

4 Answers 4

178

You want to replace it, not strip it:

s = s.replace(',', '')
Sign up to request clarification or add additional context in comments.

1 Comment

@jamylak s = re.sub(',','', s) ;)
21

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

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...
7

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

1 Comment

+2 for being a one-liner in addition to overall ridiculousness
2

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.