Write a Python program to remove punctuations from a given string


The program takes a string as input, removes all punctuation characters from the string, and then prints both the original string and the modified string to the console. The program first initializes a string variable str with a given input string that contains punctuation characters.

The program then loops through each punctuation character in the string.punctuation string using a for loop. The string.punctuation string contains all of the standard punctuation characters, such as periods, commas, semicolons, and so on.

Within the loop, the program calls the replace() method on the str variable, which replaces every occurrence of the current punctuation character with an empty string. This effectively removes all punctuation characters from the input string.

Finally, the program prints both the original input string and the modified string with the punctuation characters removed to the console using the print() function with a newline character \n and a tab character \t added for formatting.

In summary, this program takes a string as input, removes all punctuation characters from the string using the string.punctuation string and the replace() method, and then prints both the original string and the modified string to the console.

Source Code

import string
str = "Remove. punctuations @from a% given string?"
print("Original String :\n\t",str)
for c in string.punctuation:
	str = str.replace(c,"")
print("After removing Punctuations :\n\t",str)

Output

Original String :
         Remove. punctuations @from a% given string?
After removing Punctuations :
         Remove punctuations from a given string


Example Programs