Python Practice › String Programs › Program 54
Remove punctuation
Write a Python program to remove punctuation from a string.
Input
"Sqatools : is best, for python"
Output
Sqatools is best for python
Approach
- Delete every character in
string.punctuation, then tidy extra spaces.
Solution
import string text = "Sqatools : is best, for python"clean = text.translate(str.maketrans("", "", string.punctuation))print(" ".join(clean.split()))Output
Sqatools is best for python
Line by line
Line 4
The third argument of maketrans lists characters to delete.
Line 5
split() and join collapse the double space left by the removed colon.
Learn the concept
New to this topic? Read the lesson, then come back.