Python

Remove Commas From String Python

Python is a handy programming language that facilitates users with several built-in functions and operators to perform different operations, such as eliminating extra commas, characters, or any symbols from the string. Sometimes programmers want to remove the extra commas from any string that can be added previously, and now they don’t want to keep it in the code or string.

This post will provide the different techniques for removing commas from the string in Python.

How to Remove Commas From String in Python?

The following methods are used to remove commas from a string in Python:

Method 1: Remove Commas From String in Python Using “for” Loop

To remove the commas from the string, the “for” loop can be used. The “for” loop is the iterative function in Python that can be utilized for iterating over a sequence. The provided data can be a tuple, a set of instructions, a list, or a string.

Example

First, create a string variable:

original_string = "This, is, my testing, code"

 

Then, create one more empty variable that is used for storing the result of executed string which contains no commas:

resultant_string = ""

 

Now, we will traverse through the specified string character by character by utilizing a “for” loop. During the traversing, we will check through the “if” statement whether our current character is a comma or not. If the character is not a comma, then add the current character to the previously created empty string by utilizing the string concatenation “+” operation. Otherwise, the checked character will remove from the string:

for alphabet in original_string:
  if alphabet != ",":
    resultant_string = resultant_string + alphabet

 

Once the loop execution and character matches finish, we will get the output through the “print()” statement:

print(resultant_string)

 

Output

Method 2: Remove Commas From String in Python Using “re.sub()” Method

The other way to eliminate the commas from the string in Python is by utilizing the “regex()” or “re” method. The “re” term is a regular expression that is the Python’s built-in method used to deal with systematic expressions. This package contains the “re.sub()” function that performs replacement, swapping, or eliminating the commas from the strings in python.

Syntax

The syntax of the “re.sub()” function is stated below:

re.sub(oldValue, replaceValue, input_string)

 

Here:

  • oldValue” argument is used to pass the old value to the “sub()” function that needs to be eliminated.
  • replaceValue” parameter is the old value that will replace the “oldValue”.
  • input_string” argument is the string that requires to be changed to the “sub()” function.

Let’s check out the following example for better understanding!

Example

First, import the regex “re” package

import re

 

Then, call the “re.sub()” method with the required arguments and pass them to the “resultant_string

resultant_string = re.sub(",", "", original_string)

 

Lastly, use the “print()” function and get the commas eliminated modified string:

print(resultant_string)

 

It can be seen in the provided output, the resultant string has no commas:

Method 3: Remove Commas From String in Python Using “replace()” Method

To eliminate the commas from any provided string in Python, the “replace()” function can be used. It can return a duplicate string where a substring’s presences is exchanged along with another.

Syntax

The general syntax of the “replace()” method is:

str.replace(oldValue, replaceValue)

 

Here:

  • oldValue” is utilized to pass the existing character to the replace function that requires it to be removed. We will eliminate the comma as the first input parameter.
  • replaceValue” argument is utilized to pass the character, replacing the “oldValue” character.

To explore the functionality of the above-discussed method, check out the following example!

Example

We have used the previously created “original_string” variable string. Then, utilize the “replace()” method that takes the required argument and passes them to the “resultant_string” variable:

resultant_string = original_string.replace(",", "")

 

Get the modified string by utilizing the “print()” statement:

print(resultant_string)

 

Output

That’s all! We have compiled the different methods to eliminate commas from any string in Python.

Conclusion

To remove the commas from any string in Python, the iterative function “for” loop, the “re.sub()” method, and the “replace()” method can be used. The “for” loop is the iterative function that can be used for iterating over a sequence. The “re.sub()” function is used for replacement, swapping, or eliminating the commas from the strings, and the “replace()” function returns a duplicate string where a substring’s presences is exchanged along with another. This post demonstrated the ways of removing commas from the string in Python.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.