Remove commas from String in Python

Remove commas from String in Python

💡 Outline

You can remove commas 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 commas from string in Python.

Using replace() function to remove commas 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 commas 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 commas from String in Python.

Related Posts

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

  • Add character to String in Python
    21 May

    Add character to String in Python

    Table of ContentsUsing the + operatorUsing the join() functionUsing the % operatorUsing the format() functionUsing the f-strings Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different […]

  • Python find number in String
    16 May

    Python find number in String

    Table of ContentsUse of re.findall() functionUse of List Comprehension, split() and isdigit() functionsUse of split() and append() functionsUse of Numbers from String Library Extracting specific letters, phrases, words, and some specific characters is a very generic thing to do in the world of programming. In all these tasks, extraction of numbers and their occurrences from […]

  • 13 April

    How to Format Float to 2 Decimal Places in Python?

    In this article you will learn - How to Format a Float With Two Decimal Places in Python?

  • 11 April

    Python Remove Newline from List

    Table of ContentsIntroduction✨ Method 1: Using the replace() method✨ Method 2: Using re.sub()✨ Method 3: Using strip()✨ Method 4: Slice the List Elements✨ Method 5: Using List Comprehension and Splitlines Function✨ Method 6: Using str.maketrans(x,y) and str.translate(map)Conclusion 🎯 Objective: In this article, we will discuss the methods to remove ‘\n’ or newline characters from a […]

  • 03 April

    Sort String Alphabetically in Python

    In this article you will learn:- How to sort a string alphabetically in Python?

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.