In python, we use strings to analyze text data. We need to preprocess the text data before analysis. Sometimes, we might need to remove characters like commas or apostrophes from the text. In this article, we will discuss how we can remove commas from a given string in python.
Remove commas from string using for loop
The simplest way to remove a comma from a string is to create a new string leaving the comma behind.
In this approach, we will first create an empty string newString
. After that, we will traverse through the input string character by character using a for loop. While traversing, we will check if the current character is a comma or not. If the character is not a comma, we will add the present character to newString
using the string concatenation operation. Once the execution of the for loop finishes, we will get the output string free from comma character in the variable newString
.
You can observe this entire process in the following example.
myString = "This, is, a, string, that, has, commas, in, it."
print("Original String:", myString)
newString = ""
for character in myString:
if character != ",":
newString = newString + character
print("Output String:", newString)
Output:
Original String: This, is, a, string, that, has, commas, in, it.
Output String: This is a string that has commas in it.
Using the replace() method
Instead of traversing through the entire string to remove commas from the string, we can use the replace()
method to remove commas from any given string.
The syntax of the replace method is as follows.
str.replace(old_character, new_character, count)
Here,
- The parameter
old_character
is used to pass the character to the replace method that needs to be removed. We will remove the comma “,” as the first input argument. - The parameter
new_character
is used to pass the character that will replace theold_character
. We will pass an empty string as thenew_character
so that the comma character gets removed. - The parameter count is used to decide how many instances of
old_character
will be replaced by thenew_character
in the string. It is an optional parameter and we can leave it empty. In such a case, all the instances ofold_character
in the string will be replaced withnew_character
. - The
replace()
method returns the modified string after replacing all the instances ofold_character
withnew_character
.
We can use the str.replace()
method to remove commas from a given string as follows.
myString = "This, is, a, string, that, has, commas, in, it."
print("Original String:", myString)
newString = myString.replace(",", "")
print("Output String:", newString)
Output:
Original String: This, is, a, string, that, has, commas, in, it.
Output String: This is a string that has commas in it.
Remove commas from string using re.sub() method
Regular expressions are a great tool to manipulate strings. We can also use them to remove commas from a string in python. For this, we will use the re.sub()
method.
The syntax for re.sub()
method is as follows.
re.sub(old_character, new_character, input_string)
Here,
- The parameter
old_character
is used to pass the character to there.sub()
method that needs to be removed. We will remove the comma “,” as the first input argument. - The parameter
new_character
is used to pass the character that will replace theold_character
. We will pass an empty string as thenew_character
so that the comma character gets removed.
- The
input_string
parameter is used to pass the string that needs to be modified to there.sub()
method . - After execution, the method returns the modified string.
We can use the re.sub()
method to remove commas from a given string as follows.
import re
myString = "This, is, a, string, that, has, commas, in, it."
print("Original String:", myString)
newString = re.sub(",", "", myString)
print("Output String:", newString)
Output:
Original String: This, is, a, string, that, has, commas, in, it.
Output String: This is a string that has commas in it.
Conclusion
In this article, we have discussed three ways to remove commas from a string in python. To know more about strings in python, you can read this article on regular expressions in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.