How to Remove Commas From String Python

Written by kodwings | Published 2022/07/18
Tech Story Tags: python | programming | string | how-to | guide | beginners-guide | tutorial | python-tutorials

TLDRIn this article, we will learn to remove commas from strings using the Python program. We will create a program using different methods and using different functions. The program will remove the first comma from the string using replace() function. Remove n number of commas using the remove() function of Python. The function is also used to remove a specific number of substrings from a given string in Python programming. The result is a string with no commas removed from the given string and the number of comma removed.via the TL;DR App

In this article, we will learn to remove commas from strings using the Python program. We will create a program using different methods and using different functions.

Table of Contents

  • Using replace() function
  • Remove n number of comma from the strings using replace() function
  • Using regex() function
  • Conclusion

1. Remove Commas Using Replace() Function

We will use replace the () function to remove all commas from the string.

What Is Replace() Function?

replace() is an in-built function in Python, where replace() function is used to replace any characters from the given strings or is used to replace all occurrences of a substring from another substring.

So, using replace() function, we replace commas with blank; this can give strings that have no commas.

Input string = I a,m ,A,nk,ur

Output I am Ankur

Explanation

First, we will take the input of strings from the user using the input() in-built function (input() function takes a string as an input in Python).

Now, we will create a new variable named new_string. In this variable, we will use replace() function. replace() function takes two parameters, the first parameter contains substrings that we want to remove from the strings, and another parameter contains another substring for replacing the first parameter in strings.

Lastly, we will print the new_string.

Program 1

# take input of string from user
string = input("Enter strings to remove commas: ")

# use replace() function
new_string = string.replace(",", "")

# print new_string
print(new_string)

Output Enter strings to remove commas: I a,m ,A,nk,ur I am Ankur

2. Remove n Number of Commas From the Given Strings Using the remove() function

If we have to remove a specific number of commas from a given string, then we also do that using replace() in-built function of Python.

replace() function is also used to remove a specific number of substrings from a given string in Python programming.

Explanation

First, we will take the input of strings and n from the user using the input() in-built function of Python. n is the number of commas that we have to remove from the strings. Now, we will create a new variable named new_string. In this variable, we will use replace() function.

replace() function takes 3 parameters, the first parameter contains substrings that we want to remove from the strings, the second parameter contains another substring for replacing the first parameter in strings, and the last parameter is options used to remove a specific number of substrings from the given strings. Lastly, print the new_string.

Program

# take input of string from user and n 
string = input("Enter strings to remove commas: ")
n = int(input("How many commas do you want to remove from the string: "))

# use replace() function
new_string = string.replace(",", "",n)

# print new_string
print(new_string)

Output Enter strings to remove commas: I, a,m An,kur

How many commas do you want to remove from the string: 2

I am An,kur

How to Remove the First Comma From the Strings?

We will remove the first comma from the string using replace() function.

Remember what we said about replace() function taking 3 parameters.

Explanation

First, we will take the input of strings from the user using the input() in-built function of Python. Now, we will create a new variable named new_string. In this variable, we will use replace() function.

Lastly, print the new_string.

Program

# take input of string from user 
string = input("Enter strings to remove commas: ")

# use replace() function
new_string = string.replace(",", "",1)

# print new_string
print(new_string)

Output Enter strings to remove commas: I a,m Ankur I am Ankur

3. Remove Commas Using the regex() package

We will use the regex() package to remove all commas from the strings in the Python program. regex(), or re, is a Python package that contains a sub() function, which helps us remove commas from the given strings.

re.sub() function helps to replace commas with blank.

Explanation

First, we will import the re package using import. Then, we will take the input of the string using the input() in-built function (The input function used to take strings input from the user). Then, we will create a variable named new_string that uses the re.sub() function to remove all commas.

re.sub() function takes 3 arguments. The first argument takes substrings that we want to remove from the strings, the second argument takes another substring for replacing the first substring in strings, and the last argument takes the name of the string, which we want to remove all commas from it.

Then lastly, print the new_string.

Program

# import re package
import re

# take input of string from user 
string = input("Enter strings to remove commas: ")

# use re.sub|() function
new_string = re.sub(",", "", string)

# print new_string
print(new_string)

Output Enter strings to remove commas: I, a,m An,kur I am Ankur

Conclusion

In this article, we show multiple methods to remove commas from the strings. We use two methods for it, replace() and regex package, both methods are easy to implement in the program. We also see how to remove the n number of commas from the strings using replace function in Python programming with examples and explanation.

Also published here

Photo by Steve Johnson on Unsplash


Written by kodwings | Frontendscript Programming articles for programming problems
Published by HackerNoon on 2022/07/18