Python program: Remove punctuation from text
Strip punctuation marks from a sentence.
The code
import string text = "Hello!!! Is this -- finally -- working? Yes: it is." clean = "".join(ch for ch in text if ch not in string.punctuation) print(clean) table = str.maketrans("", "", string.punctuation) print(text.translate(table)) print(" ".join(clean.split())) # also squash repeated spaces
Output
Hello Is this finally working Yes it is Hello Is this finally working Yes it is Hello Is this finally working Yes it is
How it works
string.punctuation holds all ASCII punctuation. Filtering characters works anywhere; translate is faster for long text.
Python features used
Learn the ideas behind it in the free lesson Strings.