In this tutorial, I will explain how to remove commas from a string in Python. As a data scientist working with US census data, I often encounter strings containing commas that need to be removed before further processing. After researching different methods, I found several effective solutions that I will share with you in this article. I will share my findings and provide detailed examples.
Remove Commas from a String in Python
Python provides several ways to remove commas from strings. Let’s explore the most common and efficient methods with examples.
Read How to Insert a Python Variable into a String?
1. Use the replace() Method
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. Here’s an example:
name = "John, Doe"
name_without_comma = name.replace(",", "")
print(name_without_comma) Output:
John DoeI have executed the above example code and added the screenshot below.

In this example, we call the replace() method on the name string, specifying the comma as the substring to be replaced with an empty string. The resulting string, name_without_comma, has the comma removed.
Read How to Split a String by Index in Python?
2. Use the translate() Method
Another approach to removing commas from a string is by using the translate() method in combination with the str.maketrans() function. This method is useful when you want to remove multiple characters from a string. Here’s an example:
name = "Smith, Jane, Marie"
translation_table = str.maketrans("", "", ",")
name_without_commas = name.translate(translation_table)
print(name_without_commas) Output:
Smith Jane MarieI have executed the above example code and added the screenshot below.

In this example, we create a translation table using str.maketrans() , specifying an empty string for the first two arguments and the comma as the third argument. The translate() method is then called on the name string, passing the translation table. The resulting string, name_without_commas has all commas removed.
Read Convert Binary to Decimal in Python
3. Use Regular Expressions
For more advanced string manipulation, you can use regular expressions with the re module. Regular expressions provide a flexible way to match and replace patterns in strings. Here’s an example:
import re
address = "123 Main St., Anytown, USA"
address_without_commas = re.sub(r",", "", address)
print(address_without_commas) # Output: "123 Main St. Anytown USA"In this example, we use the re.sub() function to replace all occurrences of commas with an empty string. The first argument is the regular expression pattern, which matches commas, and the second argument is an empty string to replace the matches.
Read How to Split a String Every N Characters in Python?
Handle Consecutive Commas
Sometimes, you may encounter strings with consecutive commas. To remove them efficiently, you can use a combination of split() and join() methods. Here’s an example:
data = "Washington,, D.C.,"
data_without_commas = "".join(data.split(","))
print(data_without_commas) # Output: "Washington D.C."In this example, we split the data string by commas using the split() method, which results in a list of substrings. We then join the substrings back into a single string using the join() method with an empty string as the delimiter. This effectively removes all commas, including consecutive ones.
Read Python Split Regex
Conclusion
In this tutorial, I have explained how to remove commas from a string in Python. I discussed important methods like using replace() method, translate() method, regular expressions. I also discussed how to handle consecutive commas.
You may also like to read:
- How to Split a Sentence into Words in Python?
- How to Split a String and Get the Last Element in Python?
- How to Split a String into an Array in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.