How can I strip the comma from a Python string such as Foo, bar
? I tried 'Foo, bar'.strip(',')
, but it didn't work.
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.
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.
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.
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.
You want to replace
it, not strip
it:
s = s.replace(',', '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With