You want to replace it, not strip it:

s = s.replace(',', '')
Answer from eumiro on Stack Overflow
🌐
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.

Discussions

Remove Comma from Python String - Ask a Question - TestMu AI Community
How can I remove a comma from a Python string, such as “Foo, bar”? I tried using 'Foo, bar'.strip(','), but it didn’t work. How can I achieve this in Python? More on community.testmuai.com
🌐 community.testmuai.com
0
January 19, 2025
I must be an idiot or something. Why can't I strip the comma's out of a string?
Maybe you're thinking of split()? test = '12,123,123,123' test2 = test.split(',') test2 ['12','123','123','123'] Concat the elements and the commas are gone :) More on reddit.com
🌐 r/learnpython
21
6
February 13, 2012
Removing commas in a string
Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence'; More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
September 18, 2019
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
🌐
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 the string s = s.replace(",", "") print(s)
🌐
Jeremy Morgan
jeremymorgan.com › python › how-to-remove-comma-from-string-in-python
How to Remove Comma from String in Python
December 14, 2023 - 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. You can use them to search for specific patterns, such as commas, and replace them with another value.
🌐
HackerNoon
hackernoon.com › how-to-remove-commas-from-string-python
How to Remove Commas From String Python | HackerNoon
July 18, 2022 - We will use replace the () function to remove all commas from the string. replace() is an in-built function in Python, where replace() function is used to replace any characters from the given strings or is used to replace all occurrences of ...
Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 492241 › remove-commas-in-list-replacing-them-with-spaces
python - Remove commas in list replacing them with ... [SOLVED] | DaniWeb
February 23, 2015 - If you ever need an explicit string with a custom separator, converting non-string elements first (for example with map(str, ...)) and then joining is the safe approach. Avoid hacks like str(answer).replace(...) — they break if an element contains parentheses, commas, or your separator. Quick troubleshooting: check type(answer) or repr(answer) to confirm you really have a tuple/list of values. If you need the result written to a file, Python 3’s print(..., file=...) or writing the joined string to the file are both straightforward.
🌐
PythonForBeginners
pythonforbeginners.com › home › remove commas from string in python
Remove Commas From String in Python - PythonForBeginners.com
February 9, 2022 - Regular expressions are a great tool to manipulate strings. We can also use them to remove commas from a string in python.
🌐
Quora
quora.com › How-do-you-remove-the-comma-completely-in-Python
How to remove the comma completely in Python - Quora
Answer (1 of 5): Don’t put any of those variables inside repr(). You must understand what you are doing here. repr is a function that returns a representation of its input, one which hopefully can be read back as valid python code. Since string literals in python have quotes, those quotes ...
🌐
TestMu AI Community
community.testmuai.com › ask a question
Remove Comma from Python String - Ask a Question - TestMu AI Community
January 19, 2025 - How can I remove a comma from a Python string, such as “Foo, bar”? I tried using 'Foo, bar'.strip(','), but it didn’t work. How can I achieve this in Python?
🌐
Reddit
reddit.com › r/learnpython › i must be an idiot or something. why can't i strip the comma's out of a string?
r/learnpython on Reddit: I must be an idiot or something. Why can't I strip the comma's out of a string?
February 13, 2012 -

Here's my code. Simple as it seems, it is driving me crazy, and I'm sure there is a simple solution.

test = '12,123,123,123'
test2 = test.strip(',')
test2
'12,123,123,123'
test2 = test.strip('\,')
test2
'12,123,123,123'
test2 = test.strip(r',')
test2
'12,123,123,123'

What am I doing wrong? :)

Thanks,

🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to remove commas from a string python
How to Remove Commas from a String Python - Spark By {Examples}
November 7, 2024 - How to remove commas from a string in Python? To remove commas from a string you can use multiple approaches of Python. You can use some of the approaches
🌐
Finxter
blog.finxter.com › home › learn python blog › how to remove a comma from a string? 5 best ways
How to Remove a Comma from a String? 5 Best Ways - Be on the Right Side of Change
May 23, 2022 - This method uses replace() from Python’s built-in string library to remove the comma/commas and return a new string with the modifications made.
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Removing commas in a string
September 18, 2019 - Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence';
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
Source code: Lib/string/__init__.py String constants: The constants defined in this module are: Custom string formatting: The built-in string class provides the ability to do complex variable subst...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-consecutive-pairs-comma-removal
Python - Consecutive Pairs comma removal - GeeksforGeeks
April 4, 2023 - The final output is printed using the print() function and string concatenation. It displays the combined list after consecutive comma removal. ... # Python3 code to demonstrate # Consecutive Pairs Duplication Removal # using list comprehension + zip() + replace() # Initializing lists test_list1 = [1, 2, 3, 4, 5] test_list2 = [2, 3, 4, 5, 6] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Consecutive Pairs Duplication Removal # using list comprehension + zip() + replace() res = str(list(zip(test_list1, test_list2))).replace('), (', ') (') # printing result print("The combined list after consecutive comma removal : " + str(res))
🌐
CodeSpeedy
codespeedy.com › home › how to remove comma from a string in python
How to remove comma from a string in Python - CodeSpeedy
April 3, 2021 - Python consists of a built-in package called re which can be used to work with regular expressions. This package can be used to work with regular expressions. We can use the function re.sub() of this package to remove commas from the string.
🌐
Python Guides
pythonguides.com › remove-commas-from-a-string-in-python
How To Remove Commas From A String In Python?
March 19, 2025 - Read How to Insert a Python Variable into a String? The simplest way to remove commas from a string is by using the built-in replace() method. It allows you to replace a specific substring with another substring or an empty string.