Remove comma from String in Python

Remove commas from String in Python

💡 Outline

You can remove comma from string in python by using string’sreplace() method.
Syntax:

In this tutorial, we will take a sample string with a couple of commas in it and we will see some methods to remove comma from string in Python.

Using replace() function to remove comma from string in Python

There are a lot of built-in functions Python which helps us to carry various operations on strings. One of the built-in functions is the replace() function. This function is used to replace one string with another and return the new string. The changes that are to be made are mentioned in the function argument.

Example:

Output:

remove commas from this string

Explanation

  • Initialized sample string to initial_string
  • Used replace() function to replace commas in string and assigned it to another variable final_string
  • As you can see, we have replaced the comma with empty string.
  • Printed the final_string

Using re or RegEx package function to remove comma from string in Python

The re or RegEx package is a built-in python package that helps in dealing with regular expressions. This package helps in the manipulation of special sequences of characters and strings.

This package has a function called the re.sub() function which helps in replacing characters that keep on repeating again and again with another character or sub-string.
Example:

Output:

remove commas from this string

Explanation

  • Imported RegEx package that is pre-installed in python
  • Stored string in variable named initial_string
  • Used re.sub method to remove commas in string. Here, we have replaced all the commas with empty string.
  • You can either assign returned value of the re.sub method or directly print it as we did in above code.

That’s all about how to remove comma from String in Python.

Was this post helpful?

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.