Python

Remove Commas from String Python

In today’s article, we will discuss a few practices to erase commas from a python string. Python is the handy programming language that provides built-in functions to perform various operations to remove commas from a string. The regex() and replace() are one of those methods. In this tutorial, we will elucidate on all the possible ways to eliminate commas.

Example 1

In our first python program code, we use replace() method to eliminate all the commas (,) from a python string. The replace() command returns a replica of the string where a substring’s existence is exchanged with another substring. Using replace() function, we swap the commas in the python string with null elements. We further explore the functionality of replace() method with the help of Spyder Compiler.

With the help of Spyder Compiler, we create a new file to explain the concept of the replace() method. So, we first make a string that holds commas. We can then use replace() function to eliminate commas from the “h, ard ,c, ode p ,rogr, ammer” string. The print function is applied to exhibit the string without commas.

first_string="h, ard ,c ,ode, p, rogr, ammer"
string_cp = first_string.replace(',',"")
print(string_cp)

To view the functionality of the replace() function, save and execute the program file, and the output is shown on the console screen.

Example 2

Come let’s discuss our second program code, here we use the re or regex() method to eliminate all the commas (,) from a python string. Python provides a built-in method known as ‘re’ that can be utilized to deal with systematic expressions. The package ‘re’ term is a regular expression. The ‘re’ package contains the re.sub() function to erase commas from the python string.

The function re.sub() is used to swap the substring. Also, it will replace any match with the other parameter, in this case, the null string, eliminating all commas from the string. Initially, we import the ‘re’ package and then make a string that holds commas. We can then use re.sub() function in print statement to eliminate commas from the “h,ard c,ode p,rogr,ammer” string.

import re
first_string = s"h, ard c ,ode, p, rogr, ammer"
print(re.sub(",","",first_string))

Again, dry run the source file to view how the re.sub() function removes commas from the python string.

Example 3

Come let’s discuss our third program code, here we use replace() method, but this time, we remove the first comma to form a python string. Python provides built-in functions and packages to remove comma and punctuation from the python string. We use the same program file, create a programming language string, and then print our original string. After that, we call the replace() method and assign it to a variable named “altered_string”. The function takes ‘1’ as an argument to remove the first comma from the python string.

first_string = " Python, Java, C++ "
print("Your first string is:")
print(first_string)
altered_string = first_string.replace(",", "", 1)
print("Altered String is:")
print(altered_string)

Once again, save and dry run the code file to check the output on the console screen.

Example 4

Come let’s discuss our last program code, here we again use the regex() method to eliminate all the commas (,) from a python string. But in this method, we take input from a user string. Initially, we import the ‘re’ package and then create a string that takes input from the user. We can then use the re.sub() function, which takes my_string as a parameter. This function replaces all commas with null strings. The filtered string is saved in the altered variable. We can then utilize the print statement to eliminate commas from the “python, code”.

import re
my_string = input('User enter a string: ')
altered_string = re.sub(',', '', my_string)
print('My new string: {}'.format(altered_string))

Once again, save and dry run the code file to check the output on the console screen.

Conclusion

In this tutorial, we remove commas using two methods. The replace method is already placed in the string library of Python. Both approaches are very simple and easy to code. These illustrations can be altered if you want. The data can also be changed in both examples.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content