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.
Frequently Asked:
Syntax:
for iterator in inp_str: if iterator not in punctuation_string: final_string=final_string+iterator
where,
- inp_str is the actual string
- punctuation_string is the strig that has all punctuational characters
- 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.
Best Resources to Learn Python:
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:
- the first parameter is the iterator
- the second parameter specifies empty that replaces the iterator value.
It returns a new string after replacing given substring with the replacement string.
Here,
- inp_str is the actual string
- 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:
- the first parameter takes the pattern that takes an empty string
- the second parameter takes the pattern that takes an empty string
- 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,
- Regex pattern
- Replacement String
- 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'[^ws]’.
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'[^ws]',"",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'[^ws]',"",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.
Leave a Reply
You must be logged in to post a comment.