Python Program to Remove Punctuation from String

« Previous Program Next Program »

Remove Punctuation from String in Python

To remove all punctuations from a string or sentence in python, you have to ask from user to enter a string and start removing all the punctuations from that string and finally print the same string but without any punctuations as shown in the program given here.

Python Programming Code to Remove Punctuation from String

Following python program ask from the user to enter any string to remove all punctuations from it to print the string without punctuations:

# Python Program - Remove Punctuations from String
while True:
	print("Enter 'x' for exit.")
	string = input("Enter any string: ")
	if string == 'x':
		newstr = string
		print("\nRemoving punctuations from the given string...")
		punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
		for x in string.lower():
			if x in punctuations:
				newstr = newstr.replace(x,"")
		print("New string after successfully removing all punctuations!")

Here is the sample run of the above python program to illustrates how to remove all punctuations from a string:

python remove punctuations from string

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test