Python Practice › String Programs › Program 54

Remove punctuation

EasyString Programs · 54 of 106

Write a Python program to remove punctuation from a string.

Advertisement
Input
"Sqatools : is best, for python"
Output
Sqatools is best for python

Approach

  1. Delete every character in string.punctuation, then tidy extra spaces.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs