remove punctuation from string

Remove Punctuation From String in Python

In this tutorial, we will look at how to remove punctuation from a string in Python with the help of some examples.

remove punctuation from string

You can use the string constant, string.punctuation to get the set of all punctuation characters in Python.

import string
print(string.punctuation)

Output:

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

Now that we have all the punctuation characters. Let’s learn how to remove them from a string.

There are a number of ways to remove punctuation from strings in Python. For example, you can use a translation table to map punctuations in the string to None. You can also use a list comprehension to omit any punctuation characters in the string.

Let’s look at these methods with the help of examples.

Translation tables create a mapping between characters which can further be used to replace such characters. For example, let’s create a transition table to replace all occurrences of ‘a’ with ‘b’ and all occurrences of ‘m’ with ‘n’.

import string

# create translation table
tr_table = str.maketrans({'a': 'b', 'm': 'n'})
# create a string variable
s = "a monster!"
print(s)
# map using translation table
print(s.translate(tr_table))

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

a monster!
b nonster!

In the above example, we create a translation table using str.maketrans() which essentially stores the mapping of the characters and their corresponding replacement. Then, we use the string translate() function to apply the mappings in the translation table.

You can see that the characters ‘a’ and ‘m’ are replaced with the characters ‘b’ and ‘n’ respectively.

You can similarly use a translation table to remove all the punctuation characters from a string. Create a translation table with each punctuation character mapping to None and use that to remove the punctuation characters.

s = "This, right here, is what I call - A PARTY!!!"
# create a translation table
tr_table = str.maketrans({key: None for key in string.punctuation})
# remove punctuation 
s.translate(tr_table)

Output:

'This right here is what I call  A PARTY'

You can see that all the punctuation characters have been removed in the resulting string.

You can also use a list comprehension to remove punctuation. Iterate through each character in the string and only keep those characters that are not punctuations.

s = "This, right here, is what I call - A PARTY!!!"
# remove punctuation
s = ''.join([ch for ch in s if ch not in string.punctuation])
print(s)

Output:

This right here is what I call  A PARTY

Here we use a list comprehension to create a list of non-punctuation characters and then use the string join() function to join back the characters to a string. You can see that the resulting string doesn’t contain any punctuations.

Note that using a translation table is comparatively faster than using a list comprehension for removing punctuations in strings.

There are other methods as well. For example, you can use the string replace() function or use regular expressions to match and remove punctuation characters from a string.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top