codescracker


python

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

print("Enter 'x' for exit.");
string = input("Enter any string to remove all punctuations: ")
if string == 'x':
    exit();
else:
    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\n");
    print(newstr);

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

remove punctuations from string python

Now enter any string that contains some punctuations or special characters say pas$#%s!./wo*&rd. and press enter key to see the same string but without any punctuations, that will be password here after removing all the punctuations that was $#%!./*&. as shown here:

delete punctuations from string python

Same program on python shell:

python remove punctuations from string

« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test