English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program removes punctuation from the string

Complete Python Examples

This program removes all punctuation marks from the string. We will use a for loop to check each character of the string. If the character is a punctuation mark, we assign an empty string to it.

To understand this example, you should understand the followingPython programmingTopic:

Sometimes, we may want to split a sentence into a list of words.

In this case, we may first need to clean the string and remove all punctuation marks. Below is an example of how to complete this function.

Source code

# Define punctuation marks
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = "Hello!!!, he said ---and went.
# Accept user input
# 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 string without punctuation
print(no_punct)

Output result

Hello he said and went

In this program, we first define a string of punctuation marks. Then, we use a for loop to iterate over the provided string.

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

Complete Python Examples