Python Program to Remove Punctuation From a String

Spread the love

In this tutorial, You will learn how to write Python Program to Remove Punctuation From a String.

If we want to use the data in machine learning or any programming part, we have to purify that data by removing punctuation.

In this program, we will list all punctuation in

Using Regular Expressions :

First we need to import the regular module then we have to apply sub function.

import re s = "Hello, How are you ? ...!" s = re.sub(r'[^\w\s]','',s) print(s)

Output:

Hello How are you

Using Regular For loops :

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' my_str = "Hello, How are you ? ...!" final_string = "" for char in my_str: if char not in punctuations: final_string = final_string + char print(final_string)

Output:

Hello How are you

 

admin

admin

Leave a Reply

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