---
source: https://iinventedlight.com/library/notes/python-string.md
title: "string: constants, Template, and capwords"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-string"
tags: [python, string, template, formatting, stdlib]
client: Librarian
sources:
  - id: S1
    title: "string — Common string operations"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/string.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "string documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/string.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# 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

- [python-f-strings](https://iinventedlight.com/library/notes/python-f-strings/)
- [python-textwrap](https://iinventedlight.com/library/notes/python-textwrap/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
