You want to remove commas from string in python.
To remove commas from a string in python we can use the replace method or re package.
Using replace method –
The replace() method replace each matching characters from a string with a new characters.
string.replace(old, new, [, count])
old – old substring you want to replace
new – new substring with which you want to replace
count (optional) – the number of times you want to replace the old substring with the new one.
In : string = "Apple, Orange, Mango" In : string.replace(",", "") Out: 'Apple Orange Mango'
Here we replaced all commas with “” . If you want to replace only the first comma then you can use the count parameter.
In : string.replace(",", "", 1) Out: 'Apple Orange, Mango'
Here the value of count is set to 1 so it only replaced the first comma.
Using re package –
You can also use the re package to remove commas from a string in python.
In : import re In : string = "Apple, Orange, Mango" In : re.sub(",", "", string) Out: 'Apple Orange Mango'
To learn more about re.sub() method read this article – Python Regular Expression – re.sub() – search and replace string