Python – Remove Punctuations from a String

In this Python tutorial, you will learn how to remove punctuations from a string.

Table Of Contents

A string is a set of characters. Punctuations are special characters that can exist in a string. The punctuations can be any of the following characters,

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

Now our task is to delete these punctuations from the given string. There are several approaches for this task. Let’s see them one by one.

Remove punctuations from a string using for loop

In this case, we are using for loop to iterate all the characters in a string and compare each character in a string with the punctuation characters. If the any character in the input string does not exist in the punctuation string, then add that character to the new string. Finally, we can display that new string, it will all characters from original string except the punctuations.

Advertisements

Syntax:

for iterator in inp_str:
    if iterator not in punctuation_string:
        final_string=final_string+iterator

where,

  1. inp_str is the actual string
  2. punctuation_string is the strig that has all punctuational characters
  3. final_string is the new string after removing punctuations.

Example 1:

In this example, we will consider the string – “Welcome, to ‘[this]Pointer'” and remove all punctuations from it.

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

# Consider the string
inp_str= "Welcome, to '[this]Pointer'"

print("Actual String: ",inp_str)

# Iterate each character in string
for i in inp_str:
  # Check if character is not in the punctuation_string
    if i not in punctuation_string:
      #add to new string
        finalStr= finalStr + i

print("After removing punctuations: ", finalStr)

Output:

Actual String:  Welcome, to '[this]Pointer'
After removing punctuations:  WelcometothisPointer

We can see that all punctuation characters were removed from the string.

Example 2:
In this example, we will consider the string – “{Welcome}, to# ‘[this]Pointer'” and remove all punctuations from it.

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

# Consider the string
inp_str= "{Welcome}, to# '[this]Pointer'"

print("Actual String: ",inp_str)

# Iterate each character in the string
for i in inp_str:
  # Check if character is not in the punctuation_string
    if i not in punctuation_string:
      # Add to new string
        finalStr=finalStr + i

print("After removing punctuations: ", finalStr)

Output:

Actual String:  {Welcome}, to# '[this]Pointer'
After removing punctuations:  WelcometothisPointer

We can see that all punctuation symbols were removed from the string.

Remove punctuations from a string using replace() method

In this case, we are using for loop to iterate over all the characters in a punctuation string and replace the punctuation characters in an input string with an empty string. For replacement, we are going to use the replace() function of string. The replace() method takes two parameters i.e. a substring to be replaced and the replacement substring.

Syntax:

for iterator in punctuation_string:
    inp_str=inp_str.replace(iterator,"")

Parameters:

  1. the first parameter is the iterator
  2. the second parameter specifies empty that replaces the iterator value.

It returns a new string after replacing given substring with the replacement string.

Here,

  1. inp_str is the actual string
  2. punctuation_string is the strig that has all punctuational characters

It will iterate over all punctuation characters in punctuation_string. For each of the punctuation character it will replace all its occurrences with an empty string in inp_str.

Example 1:

In this example, we will consider the string – “Welcome, to ‘[this]Pointer'” and remove all punctuations from it.

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

# Consider the string
inp_str= "Welcome, to '[this]Pointer'"

print("Actual String: ",inp_str)

# Iterate each character in punctuation_string
for i in punctuation_string:
  # Replace the punctuation charcater in the input_str with empty
    inp_str=inp_str.replace(i,"")

print("After removing punctuations: ", inp_str)

Output:

Actual String:  Welcome, to '[this]Pointer'
After removing punctuations:  WelcometothisPointer

We can see that all punctuation symbols were removed from the string.

Example 2:

In this example, we will consider the string – “{Welcome}, to# ‘[this]Pointer'” and remove all punctuations from it.

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

# Consider the string
inp_str= "{Welcome}, to# '[this]Pointer'"

print("Actual String: ",inp_str)

# Iterate each character in punctuation_string
for i in punctuation_string:
  # Replace the punctuation charcater in the input_str with empty
    inp_str=inp_str.replace(i,"")
print("After removing punctuations: ", inp_str)

Output:

Actual String:  {Welcome}, to# '[this]Pointer'
After removing punctuations:  WelcometothisPointer

We can see that all punctuation symbols were removed from the string.

Remove punctuations from a string using the translate() method

The maketrans() takes the initial characters to be replaced, final characters and characters to be deleted from the string. The translation table returned by this method can be passed as a parameter inside the translate() method.

Syntax:

inp_str.translate(str.maketrans("","",punctuation_string))

Parameters:

  1. the first parameter takes the pattern that takes an empty string
  2. the second parameter takes the pattern that takes an empty string
  3. the third parameter takes the punctuation string.

where,
1. inp_str is the actual string
2. punctuation_string is the strig that has all punctuational characters

It will remove all punctuation characters from the string.

Example 1:

In this example, we will consider the string – “Welcome, to ‘[this]Pointer'” and remove all punctuations from it.

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

# Consider the string
inp_str= "Welcome, to '[this]Pointer'"

print("Actual String: ",inp_str)

# Remove punctuations from string using translate()
inp_str = inp_str.translate(str.maketrans("","",punctuation_string))

print("After removing punctuations: ", inp_str)

Output:

Actual String:  Welcome, to '[this]Pointer'
After removing punctuations:  WelcometothisPointer

We can see that all punctuation symbols were removed from the string.

Remove punctuations from a string using regular expression

The re module in Python supports sub() method, and it can be used to replace the punctuation characters with empty string. It takes three parameters,

  1. Regex pattern
  2. Replacement String
  3. Actual string

It will look for all the occurrences of substrings that matches the given regex pattern in the given string. Then it replaces all those matched substrings with the given replacement string.

We can use this to replace all the occurrences of punctuations with empty string using regex pattern r'[^\w\s]’.

Example 1:

In this example, we will consider the string – “Welcome, to ‘[this]Pointer'” and remove all punctuations from it.

import re

# Consider the string
inp_str= "Welcome, to '[this]Pointer'"

print("Actual String: ",inp_str)

# Remove punctuations from string using re
inp_str = re.sub(r'[^\w\s]',"",inp_str)

print("After removing punctuations: ", inp_str)

Output:

Actual String:  Welcome, to '[this]Pointer'
After removing punctuations:  Welcome to thisPointer

We can see that all punctuation symbols were removed from the string.

Example 2:

In this example, we will consider the string – “{Welcome}, to# ‘[this]Pointer'” and remove all punctuations from it.

import re

# Consider the string
inp_str= "{Welcome}, to# '[this]Pointer'"

print("Actual String: ",inp_str)

# Remove punctuations from string using re
inp_str = re.sub(r'[^\w\s]',"",inp_str)

print("After removing punctuations: ", inp_str)

Output:

Actual String:  {Welcome}, to# '[this]Pointer'
After removing punctuations:  Welcome to thisPointer

We can see that all punctuation symbols were removed from the string.

Summary

We have seen four different ways to remove punctuation characters from a string in Python. Happy Coding.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top