Skip to content

t-strings/pep750-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pep750-examples

This repository contains full implementations of the code examples described in PEP 750: Template Strings. It also includes a suite of tests to make sure the examples are correct.

Running This Code

You'll need Python 3.14, which was released in October 2025, and includes support for t-strings.

If you have Astral's uv installed, you can run 3.14 in an isolated environment with uv run --python 3.14 python.

You can run tests on this repository uv run pytest.

Examples

Implementing f-string Behavior

The code in fstring.py implements f-string behavior on top of t-strings, showcasing both how to work with the Template and Interpolation types, and making clear that t-strings are a generalization of f-strings:

name = "World"
value = 42.0
templated = t"Hello {name!r}, value: {value:.2f}"
formatted = f"Hello {name!r}, value: {value:.2f}"
assert f(templated) == formatted

See also the tests.

This example is described in detail in PEP 750.

Structured Logging

The code in logging.py implements two separate approaches to structured logging, showcasing how a single logger.info(t"...") call can lead to emitting both human-readable and structured (in this case, JSON-formatted) data.

The first approach follows the approach already found in the Python Logging Cookbook; the second approach defines custom Formatters that can be used to emit human-readable and structured output to different streams:

import logging
import sys

logger = logging.getLogger(__name__)
message_handler = logging.StreamHandler(sys.stdout)
message_handler.setFormatter(MessageFormatter())
logger.addHandler(message_handler)

values_handler = logging.StreamHandler(sys.stderr)
values_handler.setFormatter(ValuesFormatter())
logger.addHandler(values_handler)

action, amount, item = "traded", 42, "shrubs"
logger.info(t"User {action}: {amount:.2f} {item}")

# Outputs to sys.stdout:
# User traded: 42.00 shrubs

# At the same time, outputs to sys.stderr:
# {"action": "traded", "amount": 42, "item": "shrubs"}

See the tests in test_logging.py.

This example is described in detail in PEP 750.

Working with old-style format strings

The code in format.py shows how to convert old-style format strings intended for use with str.format() into Template instances:

from pep.format import from_format

old_style = "Thank you {name} for spending ${:.2f}."
as_template = from_format(old_style, 42, name="Alice")
assert f(as_template) == "Thank you Alice for spending $42.00."

The from_format() function supports essentially all the features of old-style format strings, including positional and keyword arguments, automatic and manual field numbering, index and dot interpolation notation, nested format specifiers, and more.

HTML Templating

There are several short "HTML templating" examples in PEP 750.

They all use a hypothetical html() function that parses template strings to an intermediate type, Element, and supports context-dependent processing of interpolations.

This repository used to contain a real working implementation of html(), but it was for demonstration purposes only. We have since built a much more robust HTML templating package, tdom, which is based on the same ideas but is much more fully featured. We hope that, in addition to being a useful package in its own right, tdom will also serve as a more complete reference implementation of the ideas described in PEP 750.

(You can find the older HTML templating examples in the commit history, in case you still want to see them.)

About

Examples of using t-strings as defined in PEP 750

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •