← All notes

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

Sources

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

Permanent ID: iil:note:cfeb9zzs

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.},
}

Markdown

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.

All artifact files (JSON index)

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.