| by Arround The Web | No comments

Remove Punctuation from String Python

Punctuation” marks are symbols that help us organize and structure sentences. They can also convey meaning, tone, and emotion. However, sometimes we may want to remove punctuation from a string in Python in the case of processing text data for natural language processing (NLP) tasks or in the case of
updating the records.

This blog will explore various ways to remove/eliminate punctuation from a Python string.

How to Remove/Eliminate Punctuation From Python String?

To remove punctuation from a string, the below methods are employed in Python:

Method 1: Remove/Delete Punctuation From Python String Using the “string.translate()” Method

A string returned by the “translate()” method contains some specific characters replaced with characters described in a dictionary/mapping table. The “str.maketrans()” method, however, is used to create a mapping table.

Syntax

string.translate(table)

 

Here, the “table” parameter specifies the table or dictionary that specifies how to perform the replacement.

Example

The below-given example code is utilized to delete/remove punctuation from the Python string:

import string
str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
print('\nAfter Removing Punctuation:',str1.translate(str.maketrans('', '', string.punctuation)))

 

In the above code:

  • The “string” is imported and initialized with punctuations.
  • The “maketrans()” method returns a mapping table that is used with the “translate()” method to replace specific characters.
  • The “string.translate()” method is used with the translation table to remove the punctuations from the string.

Output

As analyzed, the punctuations have been removed successfully.

Method 2: Remove/Delete Punctuation From Python String Using “for” Loop With the “string.replace()” Method

In this approach, the “string.replace()” method replaces the punctuations in a string based on another string (comprising the punctuations) via the “for” loop.

Syntax

string.replace(x, y, z)

 

Here, the “x”, “y”, and “z” parameters refer to the old value, new value, and the count value, respectively that specify the number of occurrences that need to be replaced.

Example

The following example code is used to remove/delete punctuation from a Python string:

str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
punc_value = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for i in str1:
    if i in punc_value:
        str1 = str1.replace(i, "
")
print('\nAfter Removing Punctuation:', str1)

 

In the above code:

  • The string containing punctuation marks is assigned and displayed.
  • A new string with all the common punctuation marks is assigned to a variable named “punc_value”.
  • The “for” loop is used to iterate over the given former string and check if any of the specified punctuation marks in the latter string is found in the former string.
  • If the punctuation marks are found, the “string.replace()” method replaces/substitutes them with an empty string.

Output

The found punctuations in the given string have been removed accordingly.

Method 3: Remove/Delete Punctuation From Python String Using the “re.sub()” Function

In Python, the “re.sub()” function of the “re” module is used to search the string/pattern and replace it with the specified replacement string.

Syntax

re.sub(pattern, repl, string, count=0, flags=0)

 

In the given syntax:

  • The “pattern” parameter defines the string/pattern to replace.
  • The “repl” parameter defines the replacement string that replaces/substitutes the original string.
  • The “string” parameters specify the original string.
  • The “count” parameter indicates the “max” number of operations to replace/substitute the particular string.
  • The “flags” parameter specifies a value that modifies the behavior of the pattern.

Example

Let’s overview the following example code:

import re
str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
new_str1 = re.sub(r'[^\w\s]', '', str1)
print('\nAfter Removing Punctuation:', new_str1)

 

In the above code lines, the “re.sub()” function takes the particular pattern that is utilized to search all the punctuation marks in the initialized string and replaces them with an empty string instead.

Output

Method 4: Remove/Delete Punctuation From Python String Using the “filter()” Function

The “filter()” function is used to filter a sequence using a specified function that tests whether each element is true or not.

Syntax

filter(function, iterable)

 

Here, “function” specifies the function that will be applied to each element of a given iterable, while the “iterable” parameter corresponds to an iterable such as a list, tuple, set, etc.

Example

The below example code is used to delete the punctuation from a Python string:

str1 = 'Python, best: guide !by Linuxhint;'
print('Given String:', str1)
output = ''.join(filter(lambda x: x.isalpha() or x.isdigit() or x.isspace(), str1))
print('\nAfter Removing Punctuation:', output)

 

In this code:

  • The “filter()” function takes the lambda function and the given string iterable as its arguments, respectively to filter the string element containing punctuation marks.
  • The “join()” method is then utilized to join/connect the returned value of the “filter()” function to an empty string.

Output

Conclusion

The “string.translate()” method, “for” loop, “re.sub()” function, or the “filter()” function is used to remove punctuation from a string. These methods replace/remove all the punctuation marks/symbols from the specified string. This Python guide presented various ways to remove punctuation from strings using appropriate examples and code explanations.

Share Button

Source: linuxhint.com

Leave a Reply