all Python 3stringterm
string
module
Character sets (letters, digits, punctuation), Template strings and the Formatter class for formatting.
Syntax
import string
String methods — upper, split, format — belong to the str type itself, not to the string module. The module offers ready character sets such as ascii_letters and digits, and the Template class with $name substitution, which is simpler and safer than format when users write the template. Python 2 also had functions like string.upper here; Python 3 does not.
Examples
import string
t = string.Template('Hello, $name!')
print(t.substitute(name='Ann')) # → Hello, Ann!
print(string.ascii_lowercase[:5], string.digits) # → abcde 0123456789
See also
Open in the full glossarydocs.python.orgReport an inaccuracy