On this page
string
Prev: statistics | Up: stdlib_module | Next: stringprep
Rel: re, difflib, textwrap, unicodedata, stringprep
Ref: docs.python.org — string
Tags: #public #python3 #stdlib
Import
import string
Overview
ASCII character-class constants (ascii_letters, ascii_lowercase, digits, hexdigits, punctuation, whitespace), safe $variable template substitution (Template), and extensible Formatter. Reach for string.Template when user-supplied templates must not access arbitrary Python attributes (unlike str.format).
Quick Start
import string
assert "a" in string.ascii_lowercase and "9" in string.digits
tmpl = string.Template("Hello, $name! Your role is ${role}.")
assert tmpl.substitute(name="Ada", role="admin") == "Hello, Ada! Your role is admin."
Implementation Walkthrough
1. Safe User-Supplied Templates (safe_substitute)
Unlike str.format() (which allows attribute/item traversal on passed objects), string.Template only substitutes explicit $identifier placeholders—and safe_substitute() leaves unknown placeholders intact instead of raising KeyError:
from string import Template
tmpl = Template("Report for $user ($date) — $missing")
rendered = tmpl.safe_substitute(user="Ada", date="2026-12-01")
assert rendered == "Report for Ada (2026-12-01) — $missing"
2. Building Character Allowlists
import string
HEX_CHARS = frozenset(string.hexdigits)
assert all(ch in HEX_CHARS for ch in "deadBEEF42")
Gotchas & Boundaries
- Never pass untrusted format strings to
str.format(): Usestring.Templatewhenever end users control the template text. - Constants are ASCII-only:
string.ascii_lettersdoes not include accented or non-Latin letters; usestr.isalpha()or unicodedata for Unicode-aware checks. - Escape literal
$inTemplateas$$.
Common Functions
- string.capwords() —
capwords(s, sep=None)— Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join. If the optional second argument sep is absent or None,
Classes & Objects
- string.Formatter —
string.Formatterclass. (Methods:.check_unused_args(),.convert_field(),.format(),.format_field(),.get_field(),.get_value()) - string.Template — A string class for supporting $-substitutions. (Methods:
.get_identifiers(),.is_valid(),.safe_substitute(),.substitute())
Constants & Attributes
string.ascii_letters(str) ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'string.ascii_lowercase(str) ='abcdefghijklmnopqrstuvwxyz'string.ascii_uppercase(str) ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'string.digits(str) ='0123456789'string.hexdigits(str) ='0123456789abcdefABCDEF'string.octdigits(str) ='01234567'string.printable(str) ='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST...string.punctuation(str) ='!"#$%&\'()*+,-./:;<=>?@[\\]^_{|}~'`string.whitespace(str) =' \t\n\r\x0b\x0c'
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/difflib
- tuts/python3/stdlib_module/re
- tuts/python3/stdlib_module/readline
- tuts/python3/stdlib_module/rlcompleter
- tuts/python3/stdlib_module/statistics
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/string/capwords
- tuts/python3/stdlib_module/string/Formatter
- tuts/python3/stdlib_module/string/Template
- tuts/python3/stdlib_module/stringprep
- tuts/python3/stdlib_module/textwrap
- tuts/python3/stdlib_module/unicodedata
Related notes
- tuts/python3/stdlib_module/difflib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/string/capwords Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/string/Formatter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/string/Template Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stringprep Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/unicodedata Linked here; links to this page; shared tags: #stdlib