2 Easy Ways To Remove Punctuation From Python Strings

Filed Under: Python String
REmoval Of Punctuation Marks From A Python String

Hello, readers! In this article, we will be focusing on the ways to remove punctuation from a Python string.

So, let us begin!!

Before diving deep into the concept of removal of punctuation marks, let us first understand the situations wherein we need to eliminate the punctuation marks.

When it comes to fetching the data values from APIs or web scraping, we often come across the data values in the JSON files which contain alphanumeric strings as well as punctuation marks. This makes the entire string seem to be a bit unformatted.

This is when we feel the need to eliminate or remove the punctuation marks from the string.

Let us now have a look at the different ways to remove the punctuation marks from the String.


1. Remove punctuation using regex library

Python provides us with regex library to deal with all kinds of regular expressions and manipulate the same.

We need to import the below library in order to work with regular expression–

import re

We will be using re.sub(pattern, replacement, original_string) to remove the punctuation marks.

re.sub(pattern, replacement, original_string)
  • pattern: The punctuation marks or the pattern of expressions we want to replace.
  • replacement: The string to be placed as a replacement for the pattern.

Example:

import re 
  
inp_str = "Hello! Folks, we are here to learn, grow and glow!!"
  
print("Original string:\n" + inp_str) 
 
opt = re.sub(r'[^\w\s]','', inp_str) 
  
print("String after deletion of punctuation marks:\n" + opt)  

Further, we have used re.sub() method to replace the punctuation marks with the replacement provided as ‘ ‘ i.e. a white space.

Output:

Original string:
Hello! Folks, we are here to learn, grow and glow!!
String after deletion of punctuation marks:
Hello Folks we are here to learn grow and glow

2. Using a Python for loop to eliminate punctuation marks

Python for loops can also be used to remove punctuation marks from a string as shown below:

  • Store all the punctuation characters in a string.
  • Create a for loop and build an if statement for every iteration such that if it detects a presence of a punctuation character, that would be replaced by a white space.

Let us try to understand the above method through an example.

Example:

import re 
  
inp_str = "Hello! Folks, we are here to learn, grow and glow!!"
  
print("Original string:\n" + inp_str) 
 
marks = '''!()-[]{};?@#$%:'"\,./^&*_'''

for x in inp_str:  
    if x in marks:  
        opt_str = inp_str.replace(x, "")  
        
print("String after deletion of punctuation marks:\n" + opt_str)  

Output:

Original string:
Hello! Folks, we are here to learn, grow and glow!!
String after deletion of punctuation marks:
Hello Folks, we are here to learn, grow and glow  

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, stay tuned @ Python with JournalDev, and till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages