Remove punctuation from String in Python

In this artilce, we will discuss different ways to remove all punctuation characters from a string in Python.

Table Of Contents

The string package in python provides a variable punctuation and it contains all the punctuation characters i.e.

r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""

We are going to use this.

Remove punctuation from String using Regex

In Python, the regex module provides a function to replace the contents of a string based on a matching regex pattern. The signature of function is like this,

sub(pattern, replacement_str, original_str)

It looks for the matches of the given regex pattern in the sting original_str and replaces all occurrences of matches with the string replacement_str.

We can use the regex.sub() function, to remove all punctuations from a string. For that we need pass pass a regex pattern that matches all the punctuations in a string. Then replace the matched substrings with empty strings.

For example,

import re
import string

strValue = "This,!! is,.  !! a ;; sample,.., ! string !!"

pattern = "[" + re.escape(string.punctuation) + "]"

strValue = re.sub(pattern, '', strValue )

print(strValue)

Output:

This is   a  sample  string

It deleted all the punctuation characters from a string in Python.

Remove punctuation from String using replace()

In Python, the string class provides a member function to replace all occurrences of a substring in a string i.e.

str.replace(to_be_replaced, replacement, count )

It accepts two arguments i.e.

  • to_be_replaced : The string to be replaced.
  • replacement : The replacement string.
  • count : The number of times sub-string will be replaced with the replacement string.

It returns a copy of the calling string object but with the changed contents i.e. after replacing all the occurrences of sub-string in that string. The original string remains unchanged.

The string package in python provides a variable punctuation and it contains all the punctuation characters i.e.

r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""

Iterate over all the characters in string.punctuation and replace all occurrences of each punctuation with an empty string.

For example,

import string

strValue = "This,!! is,.  !! a ;; sample,.., ! string !!"

for ch in string.punctuation:
    strValue = strValue.replace(ch, '')

print(strValue)

Output:

This is   a  sample  string

It deleted all the punctuation characters from a string in Python.

Remove punctuation from String using translate()

In Python, the string class provides a member function translate(). It accepts a translation table as an argument and replaces the characters in a string based on the mapping in the translation table.

We can use the maketrans() function to create this mapping in the form of a translation table. If we want to just delete some characters instead of replacement, then we can provide those characters as a third argument.

deleted all the punctuation characters from a string, create a translation table where all punctuations are mapped with empty strings. Then pass that translation table to the translate() function.

For example,

import string

strValue = "This,!! is,.  !! a ;; sample,.., ! string !!"

# Map characters to be deleted with an empty string
translation_table = str.maketrans('', '', string.punctuation)

# Remove specific characters from the string based
# on translation table / mapping table
strValue = strValue.translate(translation_table)

print(strValue)

Output:

This is   a  sample  string

It deleted all the punctuation characters from a string in Python.

Remove punctuation from String using filter() & join()

In Python, you can use the filter() function to filter all the occurrences of certain characters from a string. The steps are as follows,

  • Create a lambda function that accepts a character as an argument and returns True if the passed character matches the condition.
  • Pass this lambda function as the conditional argument to filter() function along with the string to be modified.
  • The filter() function iterates over all characters of string and yields only those characters for which lambda function returns True i.e. all characters except the character to be deleted.
  • Use join() function to combine all yeilded characters returned by filter() function.

This way, we can filter out some characters from the string based on conditions.

Create a lambda function that accepts a character as argument and check if that character is a punctuation or not. Pass that lambda function to filter() function along with the string. It will filter out all the punctuations from string. Then join back the remaining characters.

For example,

import string

strValue = "This,!! is,.  !! a ;; sample,.., ! string !!"

# Filter all digits from characters in string and create list of remaining chars
filtered_chars = filter(lambda item: item not in string.punctuation, strValue)

# Join characters in the filtered list
strValue = ''.join(filtered_chars)

print(strValue)

Output:

This is   a  sample  string

It deleted all the punctuation characters from a string in Python.

Summary

We learned about four different ways to remove punctuations from a string in Python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top