In this tutorial, we will look at how to remove punctuation from a string in Python with the help of some examples.
How to 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.
Using translation table to remove punctuation
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
Using List Comprehension to remove Punctuation
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.