How to strip down all the punctuation from a string in Python?


Stripping down all the punctuation from a string in Python can be achieved using various methods. Here are some examples:

Using string.punctuation

Example

The string.punctuation constant contains all the ASCII punctuation characters. We can use this constant to remove all the punctuation characters from the string.

We are using a list comprehension to iterate over each character in the string and check if it's not in the string.punctuation constant. If it's not a punctuation character, we add it to the no_punct variable.

import string
# sample string with punctuation
text = "Lorem, Ipsum!!!"
# remove punctuation
no_punct = "".join(char for char in text if char not in string.punctuation)
print(no_punct)

Output

Lorem Ipsum

Using Regular Expression

We can also use regular expressions to remove all the punctuation characters from the string.

Example

We are using the re.sub() method to replace all the non−word characters and non−space characters with an empty string.

import re

# sample string with punctuation
text = "Lorem, Dolor!!!"

# remove punctuation using regular expression
no_punct = re.sub(r'[^\w\s]', '', text)

print(no_punct)

Output

Lorem Dolor

Using translate()

The translate() method can also be used to remove punctuation from a string.

Example

We are creating a translation table using the str.maketrans() method and passing it to the translate() method to remove all the characters in the table from the string.

import string
# sample string with punctuation
text = "Lorem, Ipsum!!!"
# create a translation table
table = str.maketrans('', '', string.punctuation)
# remove punctuation
no_punct = text.translate(table)
print(no_punct)

Output

Lorem Ipsum

Here are some more examples of how to strip down all the punctuation from a string in Python:

Using a loop to iterate over characters

We can also loop through each character in the string and remove any punctuation characters.

Example

We are looping through each character in the string and checking if it's alphanumeric (a letter or a number) or a whitespace character. If it is, we add it to the no_punct variable. This way, we exclude any punctuation characters from the resulting string.

# sample string with punctuation
text = "Foo, Bar!!!"
# remove punctuation using a loop
no_punct = ""
for char in text:
    if char.isalnum() or char.isspace():
        no_punct += char
print(no_punct)

Output

Foo Bar

Using the replace() method

We can also use the replace() method to remove all punctuation characters from the string.

Example

We are using the replace() method to replace each punctuation character (period, comma, exclamation mark, and question mark) with an empty string. This way, we remove all the punctuation characters from the string.

# sample string with punctuation
text = "Fubar, Quz!!!"
# remove punctuation using the replace() method
no_punct = text.replace(".", "").replace(",", "").replace("!", "").replace("?", "")
print(no_punct)

Output

Fubar Quz

Using the re module with a list of punctuation characters

We can also use the re module to remove all punctuation characters from the string using a list of punctuation characters.

Example

We are using the re.sub() method to substitute all the characters that are not letters, digits, or whitespaces with an empty string. The regular expression [^a−zA−Z0−9\s]+ matches any character that is not a letter, a digit, or a whitespace character.

import re
# sample string with punctuation
text = "Foobar, Baz?"
# remove punctuation using the re module and a list of punctuation characters
no_punct = re.sub('[^a-zA-Z0-9\s]+', '', text)
print(no_punct)

Output

Foobar Baz

Updated on: 11-Aug-2023

577 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements