Open In App

Python | Remove punctuation from string

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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.



Last Updated : 11 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads