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:
Then, create one more empty variable that is used for storing the result of executed string which contains no commas:
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:
if alphabet != ",":
resultant_string = resultant_string + alphabet
Once the loop execution and character matches finish, we will get the output through the “print()” statement:
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:
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
Then, call the “re.sub()” method with the required arguments and pass them to the “resultant_string”
Lastly, use the “print()” function and get the commas eliminated modified 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:
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:
Get the modified string by utilizing the “print()” statement:
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.