Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip comma in Python string

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

like image 315
msampaio Avatar asked Apr 26 '13 09:04

msampaio


People also ask

How do I strip a comma in Python?

sub() function to erase commas from the python string. The function re. sub() is used to swap the substring. Also, it will replace any match with the other parameter, in this case, the null string, eliminating all commas from the string.

How do you remove commas from a string?

To remove all commas from a string, call the replace() method, passing it a regular expression to match all commas as the first parameter and an empty string as the second parameter. The replace method will return a new string with all of the commas removed.

How do I remove a comma colon from a string in Python?

Using str.translate() function to remove all punctuations from a string. It simply maps each character of the string through a translation table, which can be easily created with the str. maketrans() function.

How do you replace a comma in Python?

Using the replace() function to replace comma with space in list in Python. In Python, we can replace substrings within a string using the replace() function. Using this function, we can replace comma with space in list in Python. It returns a new string with the substituted substring.


1 Answers

You want to replace it, not strip it:

s = s.replace(',', '') 
like image 53
eumiro Avatar answered Sep 22 '22 05:09

eumiro