Python Program to Remove Punctuation from a String

This article covers a program in Python that removes all punctuation from a string entered by the user at runtime.

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

  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 a sample run of the above program with user input Hey!!! ?_Welcome_ {to} "codescracker"!! as string:

python program remove punctuation

Note: In the program given above, the code \' refer 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. The rest of the codes will be the same.

I wanted to include another example that does the same job as the previous program. This program might also help you understand the topic.

Python Code
import string

# function to remove punctuation from a string
def remove_punctuation(input_string):
    # create a translation table to remove punctuation
    translator = str.maketrans('', '', string.punctuation)
    
    # remove punctuation using the translation table
    return input_string.translate(translator)


# get input string from user
input_string = input("Enter a string: ")

# remove punctuation from the input string
result_string = remove_punctuation(input_string)

# print the result string without punctuation
print("String without punctuation:", result_string)
Output
Enter a string: Hello, World!
String without punctuation: Hello World

A translation table is created by the built-in Python function maketrans(). This function requires two equal-length strings as arguments, each of which represents a mapping between the characters. The replacement characters are represented by the second argument, while the replacement characters are represented by the first argument.

Another built-in Python function is translate(), which uses the translation table created by the maketrans() function to translate a given string. The translated string is returned by the translate() function, which also accepts a translation table as an argument. It substitutes the appropriate replacement character for each character in the input string that matches a character in the translation table.

Combining these two operations is a common way to change or remove particular characters or character sets from a string.

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!