Remove Commas From String Python

Remove Commas From String Python | In this article, we’ll look at how to eliminate or remove commas from a string in Python.

We will see two methods by which we can be remove commas from string Python:-
1) By the replace() function
2) By making use of the “re” package of the Python library

Let’s have a look at the following example where the string contains commas. The entire article will be based on how to proceed and remove these commas.

Example-1:
String = “Welcome, to, Know, Program”
The expected output is:- “Welcome to Know Program”

Example-2:-
string = “Hello, world!”
The expected output is: “Hello world!”

Remove Commas From String Python Using replace() Method

Python has a number of built-in functions that assist the users in executing different string manipulations. The replace() function is among the built-in functions. This function swaps out one string for another and returns the new string. The function argument specifies the adjustments that are to be done. Consider the following scenario,

Program to Remove Commas From String Python using replace() Method

string = "Hello, World!, Wel,come, to, Know, P,rogram"
mended_string = string.replace(',', "")
print(mended_string)

Output:-

Hello World! Welcome to Know Program

Let us see another program to remove commas from string Python using replace() method by taking input from the user,

string = input('Enter the string: ')
mended_string = string.replace(',', "")
print("String after removing the commas: " + mended_string)

Output:-

Enter the string: ,,,,,Know Program,,,,,
String after removing the commas: Know Program

When the commas are at the end and also at the beginning of the string, in that case, replace() method will remove all the commas from the start and end of the given string.

Enter the string: Know Program
String after removing the commas: Know Program

For the above input, the code will print the same string because replace() method doesn’t find any commas in the given string.

Remove Commas From String Python Using sub() function

The “re” or RegEx package is a built-in Python library that aids in the manipulation of regular expressions or RegEx. It aids in the handling of special characters and string sequences. It has a feature known as re.sub() that can be used to replace characters that keep repeating themselves. Look at the following example:-

import re
string = "Hello, World!, Wel,come, to, Know, P,rogram"
print(re.sub(",", "", string))

Output:-

Hello World! Welcome to Know Program

To remove the commas, we used the sub() method from the Python RegEx package. All of the commas have been replaced with an empty string in this case. One could either allocate the re.sub() method’s returned value or print it directly.

import re
string = input('Enter the string: ')
print(re.sub(",", "", string))

Output:-

Enter the string: Java, HTML, JavaScript, Python
Java HTML JavaScript Python

Enter the string: ,,,,,Know Program,,,,,
Know Program

Remove Few Commas From Given String in Python

If we want to remove only a few commas from the given string in Python then also we can use replace() method. In the replace() method, we can pass the third parameter. For example:- If we want to remove all commas from the given string till the 2nd comma occurs in the string then we have to pass 2 in the third parameter.

import re
string = "Java, HTML, JavaScript, CSS, PHP, Python"
n = int(input("Enter number: "))
print("String = " + string.replace(",", "", n))

Output for different test cases:-

Enter number: 1
String = Java HTML, JavaScript, CSS, PHP, Python

Enter number: 2
String = Java HTML JavaScript, CSS, PHP, Python

Enter number: 5
String = Java HTML JavaScript CSS PHP Python

The string contains only 5 commas but if we pass a number more than 5 then it will remove all the commas from the given string.

Enter number: 10
String = Java HTML JavaScript CSS PHP Python

If we pass any negative number then it will work like the previous test cases i.e. it will remove all the commas from the given string.

Enter number: -50
String = Java HTML JavaScript CSS PHP Python

Enter number: -1
String = Java HTML JavaScript CSS PHP Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *