Skip to content
Home » Remove Commas From String in Python

Remove Commas From String in Python

In this tutorial let’s learn about How to Remove Commas From String in Python. There are many packages and modules in Python which are helpful.

Removing a comma from a string is indirectly replacing every instance of a comma character ',' in the string with '' . In Python, we can use the replace() method or the re package to remove commas from a string.

Remove Commas From String Using replace() Python

The replace() function is one of the built-in functions in Python. In the Python str class, the replace() function replaces a substring with the provided substring and returns the altered string. This function replaces one string with another and returns the resulting string. The changes to be made are specified in the function argument.

replace() Method Syntax:

string.replace(oldvalue, newvalue, count)

oldvalueThe string to search for in the given string
newvalueThe string to replace the old value with in the specified string
countOptional. A number indicating how many occurrences of the old value should be replaced. The default value is all occurrences.

Code Sample to Remove Commas From String Using replace()

sample_string= "Sim,ilar, Geek,s Web,site"


Similar Geeks Website

It replaces all commas in the sample_string string with “”. As a result, all of the in the string sample string are eliminated.

Remove Commas From String Using re Module Python

The re or RegEx package is a built-in Python library that helps in the manipulation of regular expressions. This package aids in the manipulation of special character and string sequences.

This package includes a function called re.sub() that can be used to replace characters that keep repeating themselves with another character or sub-string.

import re
sample_string="Sim,ilar ,Geek,s Pyth,on" 


Similar Geeks Python

  • Python’s pre-installed RegEx package was imported.
  • sample_string is a variable that stores a string.
  • To remove commas from a string, I used the re.sub technique. In this case, we’ve replaced all of the commas with an empty string.
  • You can either assign the returned result of the re.sub method or print it directly, like we did in the preceding code.


In this tutorial we learnt about how to remove commas from the string using replace() and re.sub() methods in Python. Also discussed about the sample code snippets in detail.

Similar Posts:

Extract Text From Image Using pytesseract in Python
How to Convert Text to Speech in Python