Scripting

How to Remove Punctuation from a String in Python

How_to_Remove_Punctuation_from_a_String_in_Python

Strings are a combination of different characters, alphabets, numbers, special characters, and punctuation. Sometimes we need to remove punctuation marks for the processing of strings, so in this article, we‘ll discuss the method that enables us to remove punctuations from strings.

1. By using the translate() method

2. By using the re.sub() method

3. By using for loop with replace function

1. By Using the translate() method

This is one of the quickest ways to remove punctuation from a string. The translate() method takes a translation table as a parameter which is created by the maketrans() method and removes punctuation from a string.

Example:

pun = '''!()-[]{};:'"\,<>./[email protected]#$%^&*_~'''

A = 'wow! what? is this []punctuation.'

B = A.translate(str.maketrans('', '', pun))

print(B)

Text Description automatically generated

Figure 1: Using translate() method

Output:

Figure 2: Output

In figure 1, we have defined all punctuation marks as a string. To remove punctuation from string ‘A’ we create a translation table using the maketrans() method which takes three parameters: the first and second parameters are empty strings while the third parameter is a string of punctuation. Once the translation table is created translate() method removes punctuation from the string shown in figure 2.

2. By using the re.sub() method

The regular expression module has many character classes which help in selecting punctuation, thus by using the substitution function of the regular expression module punctuation can be removed from strings.

Example:

import re

A = 'wow! what? is this []punctuation.'

B = re.sub(r'[^\w\s]', '', A)

print(B)

Graphical user interface, text Description automatically generated

Figure 3: Using re.sub() function

Output:

Figure 4: Output

In the above example, we import regular expression and use the substitution function that takes three parameters: one is pattern [^\w\s] which represents characters excluding word and space, the second is a replacement which is an empty string, and the third is a string ‘A’. We see after implementing the substitution function punctuation is removed from the string in figure 4.

3. By using for loop with replace function

The replace function makes a single replacement for a character in a string, so to check all indices of the string we need a for loop. For loop will traverse through the string and make all replacements to remove punctuation.

Example 1:

import string

A = 'wow! what? is this []punctuation.'

for character in string.punctuation:

A = A.replace(character, '')

print(A)

Text Description automatically generated

Figure 5: Using replace function

Output:

Figure 6: Output

In the above example, we import string library because here we are using string.punctuation which is a set of punctuation. For removing punctuation in the string we use for loop with replace function. Replace function replaces punctuation with empty space and returns us the updated string as shown in figure 6.

Example 2:

pun = '''!()-[]{};:'"\,<>./[email protected]#$%^&*_~'''

A = 'wow! what? is this []punctuation.'

for character in pun:

A = A.replace(character, '')

print(A)

Text Description automatically generated

Figure 7: Using replace function

Output:

Figure 8: Output

In this example 2, we have declared punctuation as a string and used for loop searched for punctuation and replace values with an empty string if a match is found and get the updated string as shown in figure 8.

Similar Posts