all Python 3stringterm
string.punctuation
special characters
The string of the standard ASCII punctuation marks. Used when filtering or validating text.
Syntax
string.punctuation # '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
These are exactly 32 ASCII marks: curly quotes, em dashes and ellipsis characters are absent, so text pasted from a web page only gets half-cleaned by this constant. Underscore, on the other hand, does count as punctuation and will chop up snake_case names; for long text prefer str.translate(str.maketrans('', '', string.punctuation)) over a per-character loop.
Examples
import string
print(len(string.punctuation)) # → 32
no_punct = ''.join(c for c in 'Hello, world!' if c not in string.punctuation) # → 'Hello world'
print('!' in string.punctuation) # → True
has_punct = any(c in string.punctuation for c in 'abc!') # → True
only_punct = [c for c in 'Hi! :)' if c in string.punctuation] # → ['!', ':', ')']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy