This post will discuss how to remove punctuations from a string in Python.

The solution should remove all English punctuations from the string like a full stop, comma, double-quotes, apostrophe, a hyphen, question mark, colon, exclamation mark, and semicolon.

1. Using str.translate() function

An efficient solution is to use the str.translate() function to remove all punctuations from a string. It simply maps each character of the string through a translation table, which can be easily created with the str.maketrans() function.

Download  Run Code

 
You can also specify a dictionary to str.maketrans() function.

Download  Run Code

 
Here’s another solution that directly creates character mappings using a dictionary:

Download  Run Code

2. Using Regex

Another option is to use Regular expressions for finding and removing punctuations from a string. Here’s what the regex-based solution would look like:

Download  Run Code

 
If you have a punctuation string, you can use the following code. It uses the re.escape() function to escape special characters.

Download  Run Code

3. Using Generator Expression

Finally, you can write a simple generator expression that filters punctuation marks from a string. The following example demonstrates.

Download  Run Code

 
Here’s a solution using lambda:

Download  Run Code

That’s all about removing Punctuations from a string in Python.

 
Also See:

Remove specific characters from a Python string

Remove non-alphanumeric characters from a Python string