Jump to content

Remove Punctuation from String Python

Linux Hint

Recommended Posts

Punctuation” marks are symbols that help us organize and structure sentences. They can also convey meaning, tone, and emotion. However, sometimes we may want to remove punctuation from a string in Python in the case of processing text data for natural language processing (NLP) tasks or in the case of
updating the records.

This blog will explore various ways to remove/eliminate punctuation from a Python string.

How to Remove/Eliminate Punctuation From Python String?

To remove punctuation from a string, the below methods are employed in Python:

Method 1: Remove/Delete Punctuation From Python String Using the “string.translate()” Method

A string returned by the “translate()” method contains some specific characters replaced with characters described in a dictionary/mapping table. The “str.maketrans()” method, however, is used to create a mapping table.




Here, the “table” parameter specifies the table or dictionary that specifies how to perform the replacement.


The below-given example code is utilized to delete/remove punctuation from the Python string:

import string
str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
print('\nAfter Removing Punctuation:',str1.translate(str.maketrans('', '', string.punctuation)))


In the above code:

  • The “string” is imported and initialized with punctuations.
  • The “maketrans()” method returns a mapping table that is used with the “translate()” method to replace specific characters.
  • The “string.translate()” method is used with the translation table to remove the punctuations from the string.



As analyzed, the punctuations have been removed successfully.

Method 2: Remove/Delete Punctuation From Python String Using “for” Loop With the “string.replace()” Method

In this approach, the “string.replace()” method replaces the punctuations in a string based on another string (comprising the punctuations) via the “for” loop.


string.replace(x, y, z)


Here, the “x”, “y”, and “z” parameters refer to the old value, new value, and the count value, respectively that specify the number of occurrences that need to be replaced.


The following example code is used to remove/delete punctuation from a Python string:

str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
punc_value = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for i in str1:
    if i in punc_value:
        str1 = str1.replace(i, "
print('\nAfter Removing Punctuation:', str1)


In the above code:

  • The string containing punctuation marks is assigned and displayed.
  • A new string with all the common punctuation marks is assigned to a variable named “punc_value”.
  • The “for” loop is used to iterate over the given former string and check if any of the specified punctuation marks in the latter string is found in the former string.
  • If the punctuation marks are found, the “replace()” method replaces/substitutes them with an empty string.



The found punctuations in the given string have been removed accordingly.

Method 3: Remove/Delete Punctuation From Python String Using the “re.sub()” Function

In Python, the “re.sub()” function of the “re” module is used to search the string/pattern and replace it with the specified replacement string.


re.sub(pattern, repl, string, count=0, flags=0)


In the given syntax:

  • The “pattern” parameter defines the string/pattern to replace.
  • The “repl” parameter defines the replacement string that replaces/substitutes the original string.
  • The “string” parameters specify the original string.
  • The “count” parameter indicates the “max” number of operations to replace/substitute the particular string.
  • The “flags” parameter specifies a value that modifies the behavior of the pattern.


Let’s overview the following example code:

import re
str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
new_str1 = re.sub(r'[^\w\s]', '', str1)
print('\nAfter Removing Punctuation:', new_str1)


In the above code lines, the “re.sub()” function takes the particular pattern that is utilized to search all the punctuation marks in the initialized string and replaces them with an empty string instead.



Method 4: Remove/Delete Punctuation From Python String Using the “filter()” Function

The “filter()” function is used to filter a sequence using a specified function that tests whether each element is true or not.


filter(function, iterable)


Here, “function” specifies the function that will be applied to each element of a given iterable, while the “iterable” parameter corresponds to an iterable such as a list, tuple, set, etc.


The below example code is used to delete the punctuation from a Python string:

str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
output = ''.join(filter(lambda x: x.isalpha() or x.isdigit() or x.isspace(), str1))
print('\nAfter Removing Punctuation:', output)


In this code:

  • The “filter()” function takes the lambda function and the given string iterable as its arguments, respectively to filter the string element containing punctuation marks.
  • The “join()” method is then utilized to join/connect the returned value of the “filter()” function to an empty string.




The “string.translate()” method, “for” loop, “re.sub()” function, or the “filter()” function is used to remove punctuation from a string. These methods replace/remove all the punctuation marks/symbols from the specified string. This Python guide presented various ways to remove punctuation from strings using appropriate examples and code explanations.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...