Python Program to Remove Punctuations From a String

In this example, we will write a program to remove punctuations from a string input by the user. To better understand this example, make sure you have knowledge of the following tutorials:-

Python Program to Remove Punctuations From a String

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
input_str = input("Enter a string: ")

final_str = ""
for char in input_str:
    if char not in punctuations:
        final_str += char

print(final_str)

The output of the above program is:-

Enter a string: Hello !!! Good Morning
Hello Good Morning
Program Explanation:
The list of punctuation is defined at first. The input string taken from the user is iterated in a loop where the individual character is checked against the list of punctuation using no int operator in Python. If the punctuation is not present in the character then it is added to a new string. The final string is printed in the screen.
SHARE Python Program to Remove Punctuations From a String

You may also like...

Leave a Reply

Your email address will not be published.

Share