Python Program to Remove Punctuations From a String

To understand this example, you should have the knowledge of the following Python programming topics:

Sometimes, we may wish to break a sentence into a list of words.

In such cases, we may first want to clean up the string and remove all the punctuation marks. Here is an example of how it is done.

Source Code

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string


Hello he said and went

In this program, we first define a string of punctuations. Then, we iterate over the provided string using a for loop.

In each iteration, we check if the character is a punctuation mark or not using the membership test. We have an empty string to which we add (concatenate) the character if it is not punctuation. Finally, we display the cleaned up string.

Also Read:

Did you find this article helpful?