Home » Python » Remove a Comma from a String in Python

Remove a Comma from a String in Python

To remove a comma from a string in Python, use the str.replace() method.

str.replace() in Python replaces the specified phrase in the string with the new phrase. Use the replace() method to remove all instances of a comma from a string. For example, replacing all instances of character ‘,’ with ”

In this example, we will discuss how to use the str.replace() method in Python to remove commas from string in Python.

Use str.replace() to remove comma from a string

To remove a comma from a string in Python:

  1. Call the str.replace() on the string and pass a comma to search for a replacement and a new character for a replacement.
  2. The replace() returns a copy of the string in which all occurrences of a commas are replaced by a new character ”.
str = "Remove , all occurrences, of a commas, from a String!"

str1 = str.replace(',','')

In the above Python program, we pass the following parameters to replace():

  1. str variable stores the string that contains commas in it.
  2. Use the str.replace() on the str to remove commas from a string.
  3. character ‘,’ to search for in the string.
  4. replacement character ”. It will replace all occurrences of commas ‘,’ with ”

It will return the copy of the string and store it in the str1 variable and print using print() Python.

The output of the above program after removing commas from a string is:

Remove  all occurrences of a commas from a String!

Cool Tip: How to remove all occurrences of a character in a string Python!


I hope the above article on how to remove commas from a string in the Python using str.replace() method is helpful to you.

You can find more topics about the Python tutorials on the DataVisualizr Home page.

Leave a Comment