PEP 750 – Template Strings (t-strings) have been accepted
a year ago
- #Template Strings
- #String Processing
- #Python
- PEP 750 introduces template strings (t-strings) as a generalization of f-strings, allowing custom string processing before combining interpolated values.
- Template strings evaluate to a new immutable type `Template` in the `string.templatelib` module, providing access to string parts and interpolations.
- Interpolations are represented by the `Interpolation` type, which includes attributes like `value`, `expression`, `conversion`, and `format_spec`.
- Template strings support concatenation, raw strings (with `rt` or `tr` prefix), and the debug specifier (`=`), similar to f-strings.
- Use cases include security-sensitive string processing (e.g., HTML escaping, SQL injection prevention), structured logging, and domain-specific languages.
- The PEP rejects delayed evaluation of interpolations, arbitrary string prefixes, and binary template strings, focusing on simplicity and compatibility with f-strings.