Why a regex?

mystring = mystring.replace(",", "")

is enough.

Of course, if you insist:

mystring = re.sub(",", "", mystring)

but that's probably an order of magnitude slower.

Answer from Tim Pietzcker on Stack Overflow
Discussions

Efficient Way To Remove Repeating Commas From String
', '.join([x.strip() for x in my_string.split(',') if not x.isspace() and x != '']) More on reddit.com
🌐 r/learnpython
9
2
December 1, 2022
Replacing comma in Python - Stack Overflow
I am having a problem stripping commas out of a string while doing some web scraping. My code is as follows. import urllib import re htmlfile = urllib.urlopen ("http://example.com") htmltext = More on stackoverflow.com
🌐 stackoverflow.com
May 8, 2013
python - re.sub replace spaces with comma - Stack Overflow
I have a list of items which look like so: 2.4 -2.0 4.3 -6.0 12.5 1.0 What I would like is to remove all those spaces and replace them with "," (comma) except for ... More on stackoverflow.com
🌐 stackoverflow.com
python - Is there a simple way to replace a comma with nothing? - Stack Overflow
replace is a string method. It can only be used on a string, not a list of strings (or list of anything else for that matter). I've updated my answer with an example of how to read each line of the file in as a string and do something to it a bit more easily. Hopefully that helps. 2014-02-20T03:21:09.567Z+00:00 ... Check out this: How do I use Python to convert a string to a number if it has commas ... More on stackoverflow.com
🌐 stackoverflow.com
February 20, 2014
🌐
CodeSpeedy
codespeedy.com › home › replace comma with a new line in a text file using python
Replace comma with a new line in a text file using Python - CodeSpeedy
January 22, 2020 - f1=open("Desktop/file1.txt","r+") input=f1.read() print(input) input=input.replace(',','\n') print(input) f2=open("Desktop/file1.txt","w+") f2.write(input) f1.close() f2.close()
🌐
ItSolutionstuff
itsolutionstuff.com › post › python-string-replace-space-with-comma-exampleexample.html
Python String Replace Space with Comma Example - ItSolutionstuff.com
October 30, 2023 - import re myString = ' 23 23 45' # python string replace space with comma newString = re.sub("\s+", ",", myString.strip()) print(newString)
🌐
Stack Abuse
stackabuse.com › how-to-remove-commas-from-a-string-in-python
How to Remove Commas from a String in Python
June 12, 2023 - Python's built-in string method replace() is an easy-to-use function for removing characters from a string. The replace() method replaces a specified phrase with another specified phrase, and it's a perfect fit for our problem: # Given string s = "H,e,l,l,o, W,o,r,l,d" # Removing commas from ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-swap-commas-dots-string
Python – Swap commas and dots in a String | GeeksforGeeks
December 27, 2024 - Let's understand different methods to swap commas and dots in a string. ... str.replace() allows us to swap characters by first replacing them with a temporary placeholder.
🌐
Jeremy Morgan
jeremymorgan.com › python › how-to-remove-comma-from-string-in-python
How to Remove Comma from String in Python
December 14, 2023 - string = "hello, world" print(string.replace(",", "")) ... This will remove all commas from the string and print the result. Regular expressions are a powerful tool for working with text data in Python.
🌐
GeeksforGeeks
geeksforgeeks.org › replace-commas-with-new-lines-in-a-text-file-using-python
Replace Commas with New Lines in a Text File Using Python - GeeksforGeeks
May 31, 2024 - For example, If the file is small, you can use readlines() or a loop approach in Python. Input: line 1 line 2 line 3 Output: ... Making a comma-separated string from a list of strings consists of combining the elements of the list into a single string with commas between each element.
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › re › python-re-exercise-31.php
Python: Replace all occurrences of space, comma, or dot with a colon - w3resource
July 22, 2025 - Write a Python program to scan a text and replace each instance of a space, comma, or dot with a colon using regex.
🌐
EXLskills
exlskills.com › courses › methods in python › methods in python
Replace and Strip | Methods in Python - EXLskills
November 29, 2018 - # Replace the comma string_1 = string.replace(',','') # Replace the period string_2 = string_1.replace('.','') print(string_2) This gives back Hello World! Lets get rid of some bad punctuation! Another useful method that we can use with strings is called .strip(). This will remove specific characters from the end of a string string that we do not want. For example, if you are trying to count the occurrences of words in a string, you want to ignore all the periods and exclamation marks.
🌐
GeeksforGeeks
geeksforgeeks.org › python-replace-delimiter
Python – Replace delimiter | GeeksforGeeks
April 10, 2023 - Replace the comma delimiter in the string ele with the replacement delimiter repl_delim using the replace() method. Append the modified string to the res list. Print the modified list res.
🌐
Reddit
reddit.com › r/learnpython › efficient way to remove repeating commas from string
r/learnpython on Reddit: Efficient Way To Remove Repeating Commas From String
December 1, 2022 -

I have a string that consists of something like:

my_string = 'Now is the time, , for all good men,   , ,to come to the aid,, of their party'

...and I want to keep only a single comma for each repeating set of commas:

result = 'Now is the time, for all good men, to come to the aid, of their party'

I've looked a numerous methods to remove sequential characters, but 1) these are not sequential (may have one or more blanks between them); and 2) I only want to remove the extra commas and not other repeating characters that might appear in the string.

Any help would be greatly appreciated.

🌐
Delft Stack
delftstack.com › home › howto › python › remove commas from string in python
How to Remove Commas From String in Python | Delft Stack
February 2, 2024 - To remove comma from a string in Python, we can use the str.replace() method and the re package.
🌐
HackerNoon
hackernoon.com › how-to-remove-commas-from-string-python
How to Remove Commas From String Python | HackerNoon
July 18, 2022 - In this variable, we will use replace() function. replace() function takes two parameters, the first parameter contains substrings that we want to remove from the strings, and another parameter contains another substring for replacing the first ...