Open In App

Python | Remove punctuation from string

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Many times while working with Python strings, we have a problem in which we need to remove certain characters from strings. This can have applications in data preprocessing in the Data Science domain and also in day-day programming. Let’s discuss certain ways in which we can perform this task using Python.

Example

Input: 'Gfg, is best: for ! Geeks ;'
Output: Gfg is best for Geeks
Explanation: Here we can observe the difference between input and output we removed all the
punctuation from the input and the ways to this is listed below to do that.

Ways to Remove Punctuation from a String

There can be many ways to remove the punctuation from a string but the main ones are listed below. So let’s explore them one by one. Below are the methods that we will cover in this article:

  • Remove Punctuation from a String with Translate
  • Remove Punctuation from a String with a Python loop
  • Remove Comma from a String with a Python loop
  • Remove Punctuation from a String with regex 
  • Using for loop, punctuation string, and not in operator
  • Removing Punctuation from a String with filter()
  • Using the replace() method

Remove Punctuation from a String with Translate

The first two arguments for string.translate method is empty strings, and the third input is a Python list of the punctuation that should be removed. This instructs the Python method to eliminate punctuation from a string. This is one of the best ways to strip punctuation from a string.

Python3




import string
 
test_str = 'Gfg, is best: for ! Geeks ;'
 
test_str = test_str.translate
    (str.maketrans('', '', string.punctuation))
print(test_str)


Output:

Gfg is best for  Geeks 

Remove Punctuation from a String with a Python loop

This is the brute-force way in which this task can be performed. In this, we check for the punctuations using a raw string that contain punctuations and then we construct a string removing those punctuations.

Python3




# initializing string
test_str = "Gfg, is best : for ! Geeks ;"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing punctuations string
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
 
# Removing punctuations in string
# Using loop + punctuation string
for ele in test_str:
    if ele in punc:
        test_str = test_str.replace(ele, "")
 
# printing result
print("The string after punctuation filter : " + test_str)


Output: 

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best for Geeks

Time complexity: O(n)
Auxiliary space: O(n), where n is the number of characters in the string.

Remove comma from a String with a Python loop

This is the brute way in which this task can be performed. In this, we check for the comma using a raw string that contains commas and then we construct a string removing those commas.

Python3




def remove_commas(string):
    result = ""
    for char in string:
        if char != ",":
            result += char
    return result
   
input_string = "GFG, is, the, best."
output_string = remove_commas(input_string)
print(output_string)


Output:

GFG is the best

Remove Punctuation from a String with regex 

The part of replacing punctuation can also be performed using regex. In this, we replace all punctuation with an empty string using a certain regex.

Python3




import re
 
# initializing string
test_str = "Gfg, is best : for ! Geeks ;"
 
# printing original string
print("The original string is : " + test_str)
 
# Removing punctuations in string
# Using regex
res = re.sub(r'[^\w\s]', '', test_str)
 
# printing result
print("The string after punctuation filter : " + res)


Output : 

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best for Geeks

Using for loop, punctuation string, and not in operator

Here, we will see Removing punctuations in string using loop + punctuation string.

Python3




# initializing string
test_str = "Gfg, is best : for ! Geeks ;"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing punctuations string
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
res=" "
 
for ele in test_str:
    if ele not in punc:
        res+=ele
         
# printing result
print("The string after punctuation filter : " + res)


Output

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 












The Time and Space Complexity for all the methods are the same:

Time complexity: O(n)
Auxiliary space: O(n)

Removing Punctuation from a String with filter()

The filter() method filters the elements of a sequence based on a given condition.
In this case, we can use the filter() method and a lambda function to filter out punctuation characters.

Python3




def remove_punctuation(test_str):
# Using filter() and lambda function to filter out punctuation characters
  result = ''.join(filter(lambda x: x.isalpha() or x.isdigit() or x.isspace(), test_str))
  return result
 
test_str = "Gfg, is best : for ! Geeks ;"
print("The original string is : " + test_str)
 
result = remove_punctuation(test_str)
print("The string after punctuation filter : " + result)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 












Time complexity: O(n)
Auxiliary space: O(n)

Removing Punctuation from a String using the replace() method

Import the string module then initialize the input string and print the original string. Loop through each punctuation character in the string punctuation constant after it uses the replace() method to remove each punctuation character from the input string. and then print the resulting string after removing punctuations.

Python3




import string
 
# initializing string
test_str = "Gfg, is best : for ! Geeks ;"
 
# printing original string
print("The original string is : " + test_str)
 
# Removing punctuations using replace() method
for punctuation in string.punctuation:
    test_str = test_str.replace(punctuation, '')
 
# printing result
print("The string after punctuation filter : " + test_str)


Output

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 












Time Complexity Analysis: O(len(string.punctuation) * len(test_str)) as the for loop iterates through all the punctuation characters in the string.punctuation constant, which takes O(len(string.punctuation)) time.

Auxiliary Space Analysis: O(1) . Because the input string is modified in place, so no extra space is required for storing the result.



Similar Reads

Python - Remove Punctuation Tuples
Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of all the tuples which contain punctuation in tuples. This kind of problem can occur in data filtering applications. Let's discuss certain ways in which this task can be performed. Input : test_list = [('.', ', '), ('!', 8)] Output : [] Input
8 min read
string.punctuation in Python
In Python3, string.punctuation is a pre-initialized string used as string constant. In Python, string.punctuation will give the all sets of punctuation. Syntax : string.punctuation Parameters : Doesn't take any parameter, since it's not a function. Returns : Return all sets of punctuation. Note : Make sure to import string library function inorder
1 min read
Python program to Sort Strings by Punctuation count
Given the Strings list, sort by punctuations count. Input : test_list = ["gfg@%^", "is", "Best!"] Output : ['is', 'Best!', 'gfg@%^'] Explanation : 0 &lt; 1 &lt; 3, sorted by punctuation count. Input : test_list = ["gfg@%^", "Best!"] Output : [ 'Best!', 'gfg@%^'] Explanation : 1 &lt; 3, sorted by punctuation count. Method #1 : Using string.punctuati
3 min read
Extract punctuation from the specified column of Dataframe using Regex
Prerequisite: Regular Expression in Python In this article, we will see how to extract punctuation used in the specified column of the Dataframe using Regex. Firstly, we are making regular expression that contains all the punctuation: [!"\$%&amp;\'()*+,\-.\/:;=#@?\[\\\]^_`{|}~]* Then we are passing each row of specific column to re.findall() functi
2 min read
Python - Remove front K characters from each string in String List
Sometimes, we come across an issue in which we require to delete the first K characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus. Let’s discuss certain ways in which this can
6 min read
Python - Remove String from String List
This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certain way outs to solve this problem. Method #1: Usin
4 min read
How to Remove Letters From a String in Python
Strings are data types used to represent text/characters. In this article, we present different methods for the problem of removing the ith character from a string and talk about possible solutions that can be employed in achieving them using Python. Input: 'Geeks123For123Geeks'Output: GeeksForGeeksExplanation: In This, we have removed the '123' ch
6 min read
Remove Special Characters from String Python
The generic problem faced by the programmers is to remove unwanted characters from a string using Python. But sometimes the requirement is way above and demands the removal of more than 1 character, but a list of such malicious characters. These can be in the form of special characters for reconstructing valid passwords and many other applications
6 min read
Python | Ways to remove numeric digits from given string
Given a string (may contain both characters and digits), write a Python program to remove the numeric digits from string. Let's discuss the different ways we can achieve this task. Method #1: Using join and isdigit() C/C++ Code # Python code to demonstrate # how to remove numeric digits from string # using join and isdigit # initialising string ini
6 min read
Python | Remove the given substring from end of string
Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Remove the substring from the end of the string using Slicing In this method, we are using string slicing to remove th
3 min read
Practice Tags :