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

  • 29 November

    Prefix b Before String in Python

    Table of ContentsPrefix b Before String in PythonConclusion Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and […]

  • 28 November

    Get String Between Two Characters in Python

    Table of ContentsUsing the string-slicing techniqueUsing Regular ExpressionsUsing the split() functionConclusion Using the string-slicing technique To get String between two characters in Python: Use String’s find() method to find indices of both the characters. Use String’s slicing to get String between indices of the two characters. [crayon-638b2ba932311214208081/] Output: ava2Blo In the above example, we found […]

  • 25 November

    Print String Till Character in Python

    Table of ContentsPrint String Till Character in PythonUsing the for loop and break statementUsing the slicing techniqueUsing the split() functionUsing the partition() functionConclusion A string can be termed as a collection of characters with each character occupying a particular position. Strings cannot be altered and are considered immutable in Python. Print String Till Character in […]

  • 25 November

    Remove NewLine from String in Python

    Table of ContentsRemove NewLine from String in PythonUse str.replace() to Remove Newline Characters From a Python StringUse str.strip() to Remove Newline Characters From a Python StringUse str.splitlines() to Remove Newline Characters From a Python StringUse re.sub() to Remove Newline Characters From a Python StringRemove Newline from a List of Strings in Python 💡 TL;DR You […]

  • 24 November

    Print Variable and String in Same Line in Python

    Table of ContentsPrint Variable and String in Same Line in PythonUsing a commaUsing the % formattingUsing the format() functionUsing the fstringsUsing the + operator and str() functionConclusion 💡 TL;DR To Print Variable and String in Same Line in Python, use print() method and separate String and variable with comma. [crayon-638b2ba917152017989547/] Print Variable and String in […]

  • 16 November

    Print None as Empty String in Python

    Table of ContentsHow To Print None as An Empty String in Python?Using the Boolean OR operator.Using a lambda function along with the Boolean OR operator.Using an if statement.Conclusion. The term None in python is not the same as an empty string or 0. None is a whole data type in itself that is provided by […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.