codescracker
python

Python Program to Remove Punctuation from String



This article covers a program in Python that removes all punctuation from a string, entered by user at run-time of the program.

The program given below deals with 14 punctuation marks that are available in English language. The list of all 14 punctuation marks are:

  1. Period ()
  2. Question Mark ( ? )
  3. Exclamation Point ( ! )
  4. Comma ( , )
  5. Colon ( : )
  6. Semicolon ( ; )
  7. Dash ( - )
  8. Hyphen ( _ )
  9. Brackets ( [ ] )
  10. Braces ( { } )
  11. Parentheses ( ( ) )
  12. Apostrophe ( ' )
  13. Quotation Marks ( " )
  14. Ellipsis ( ... )

The question is, write a Python program to remove punctuation from a given string. The program given below is its answer:

print("Enter the String: ", end="")
str = input()

punctuation = ".?!,:;-_[]{}()\'\"..."

for ch in str:
    if ch in punctuation:
        str.replace(ch, "")

print("\nString without Punctuation:", str)

The snapshot given below shows the sample run of above program with user input Hey!!! ?_Welcome_ {to} "codescracker"!! as string:

python program remove punctuation

Note - In the program given above, the code \' refers to '. Where are the code \" refers to ".

To remove some other characters too, such as &, <, >, @, $, # etc. then initialize all these characters to the variable say punctuation. Rest of all the codes will be same.

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker