string: constants, Template, and capwords
Answer: The constants defined in this module include ascii_letters, digits, and punctuation. Template strings provide $-substitutions. substitute() raises KeyError if a placeholder is missing. safe_substitute() leaves missing placeholders in the result.
Constants and capwords
The constants defined in this module are ascii_letters, digits, and punctuation.
ascii_letters is ascii_lowercase written next to ascii_uppercase.
digits is the string '0123456789'.
punctuation holds the ASCII punctuation characters of the C locale.
whitespace holds the ASCII whitespace characters.
printable joins digits, ascii_letters, punctuation, and whitespace.
capwords() splits on str.split(), capitalizes each piece with str.capitalize(), and joins the result with str.join().
Template and Formatter
Template strings, also called $-strings, follow the substitutions in PEP 292.
$$ is an escaped $, and $identifier names a substitution placeholder.
substitute() fills placeholders from a mapping and raises KeyError when one is missing.
safe_substitute() is the same idea, except a missing placeholder stays in the result instead of raising KeyError.
The Formatter class lets you create and customize your own string formatting behaviors using the same implementation as format().
See also
Claims
- id: C1 claim: ascii_letters combines ascii_lowercase and ascii_uppercase. source: S1 evidence: "concatenation of the ascii_lowercase and ascii_uppercase"
- id: C2 claim: digits is the string 0123456789. source: S1 evidence: "string '0123456789'"
- id: C3 claim: capwords splits, capitalizes, and rejoins words. source: S1 evidence: "capitalize each word using str.capitalize()"
- id: C4 claim: Template strings use $-substitutions as in PEP 292. source: S2 evidence: "Template strings ($-strings)"
- id: C5 claim: safe_substitute leaves missing placeholders in place instead of raising KeyError. source: S2 evidence: "instead of raising a :exc:
KeyError"
Sources
- [S1] string — Common string operations. Python Software Foundation. https://docs.python.org/3/library/string.html . License: PSF License Version 2. Accessed 2026-11-23.
- [S2] string documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/string.rst . License: PSF License Version 2. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, string — Common string operations; CPython, string documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, string — Common string operations; CPython, string documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "string: constants, Template, and capwords." iinventedlight Library, iil:note:cfeb9zzs, version of 2026-11-23. https://iinventedlight.com/library/notes/python-string/. License: CC-BY-4.0.
@misc{iil_note_cfeb9zzs,
author = {{iinventedlight}},
title = {{string: constants, Template, and capwords}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/python-string/},
note = {Permanent ID iil:note:cfeb9zzs. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-string.schema.json (schema; CC-BY-4.0)
- python-string.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-string.md.