Javaexercise.com

Python String: Remove Punctuation From String

This article is all about removing punctuation marks from the string in Python.

We are familiar with some common punctuation marks like comma(,), question mark(?), hyphen(-), etc.

Removing these punctuations is quite easy, Python provides a string module that contains several methods to deal with these problems.

Let's see some examples.

Example: Remove Punctuation from String using translate() Method in Python

Here, we used the translate() method of the string module to get a new string after removing all the punctuation marks.

The translate() method takes either a dictionary or a mapping table to perform the character replacement.

Look at the given code example and output.

import string
# Take a string with punctuation
s = "String - may? have: Punctuations,"
print(s)
# Remove punctuation
result = s.translate(str.maketrans('','',string.punctuation))
# Display result
print(result)

Output

String - may? have: Punctuations,

String  may have Punctuations

 

Example: Remove Punctuation from String using string.punctuation constant in Python

The string.punctuation constant is a built-in constant in Python that contains a set of punctuations.

First, we collect all the punctuations into a variable and then join characters after comparing them to the join() method.

This is a single-line statement to remove punctuations from the string in Python.

import string
# Take a string with punctuation
s = "String - may? have: Punctuations,"
print(s)
# Remove punctuation
s2 = set(string.punctuation)
result = ''.join(ch for ch in s if ch not in s2)
# Display result
print(result)

Output

String - may? have: Punctuations,

String  may have Punctuations

 

Example: Remove Punctuation from String using regex in Python

In this example, we used the compile() method of regex to remove punctuation from the string in Python.

See the below code example and output.

import string
import re
# Take a string with punctuation
s = "String - may? have: Punctuations,"
print(s)
# Remove punctuation
s2 = re.compile('[%s]' % re.escape(string.punctuation))
result = s2.sub('', s)
# Display result
print(result)

Output

String - may? have: Punctuations,

String  may have Punctuations

 

Example: Remove Punctuation from String using regex.sub() Method in Python

The sub() method of the regex module takes a regex pattern as an argument that can be used to remove punctuation from the string.

import string
import re
# Take a string with punctuation
s = "String - may? have: Punctuations,"
print(s)
# Remove punctuation
result = re.sub(r'[^\w\s]','',s)
# Display result
print(result)

Output

String - may? have: Punctuations,

String  may have Punctuations

 

Example: Remove Punctuation from String using the replace() Method in Python

This is another approach to remove punctuation from the string.

Here, we used the replace() method inside the loop to check for each character.

If the character is punctuation char then it is replaced, else no change is required. 

import string
import re
# Take a string with punctuation
s = "String - may? have: Punctuations,"
print(s)
# Remove punctuation
for ch in string.punctuation:
    s = s.replace(ch,"")
# Display result
print(s)

Output

String - may? have: Punctuations,

String  may have Punctuations

 

Example: Remove Punctuation from String using the lambda in Python

If you are comfortable and like to use lambda then use the below code to remove punctuation from a string.

The use of lambda makes code concise and short.

import string
import re
# Take a string with punctuation
s = "String - may? have: Punctuations,"
print(s)
# Remove punctuation
result = lambda x: ''.join([i for i in x if i not in string.punctuation])
# Display result
print(result(s))

Output

String - may? have: Punctuations,

String  may have Punctuations