Python Program to Remove Punctuations From a String

Posted in

Python Program to Remove Punctuations From a String
vinaykhatri

Vinay Khatri
Last updated on December 19, 2024

    Here in this article, we have provided python source code which can remove all punctuations from a string.

    Prerequisite topics to create this program

    • Python For loop
    • Python String
    • Python in operator
    • Python if...else statements

    Steps

    • Ask the user to enter a string.
    • Create a string that contains all the punctuations.
    • create an empty string.
    • now loop through the user entered string and add only those characters in the empty string which are not punctuations.

    Python Program to Remove Punctuations From a String

    Code

    string = input("Enter the String: ")
    punc= '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    new_string=""
    for i in string:
        if  not i in punc:
            new_string+=i
    
    print("The String without puctuation is: \n", new_string) 

    Output 1

    Enter the String: Hello !! this is tech geek buzz **** and we welcome you all-------
    The String without punctuation is: 
    Hello this is tech geek buzz and we welcome you all

    Output 2

    Enter the String: **TehcGeekBuzz!!! Welcome User!!!^&
    The String without punctuation is:
    TehcGeekBuzz Welcome User

    People are also reading:

    Leave a Comment on this Post

    0 Comments