News

Python 3.14 to Introduce Template Strings (t-strings) for Enhanced String Processing

Python 3.14 to Introduce Template Strings (t-strings) for Enhanced String Processing

April 10, 2025
Python PEP 750 Template Strings t-strings string processing Python 3.14
PEP 750 introduces Template Strings (t-strings) in Python 3.14, offering developers greater control over interpolated values for safer and more flexible string processing.

PEP 750 – Template Strings (t-strings) Accepted in Python

String formatting comparison | Pydon't 🐍 | mathspp

PEP 750, which introduces Template Strings (t-strings), has been officially accepted and is set to be included in Python 3.14. This new feature provides a powerful and flexible way to handle string processing, offering developers more control over interpolated values before they are combined into a final string.

Key Features of t-strings

  • Template String Literals: t-strings are defined using the t prefix, similar to f-strings. They evaluate to a new type, Template, which allows access to both the string and its interpolated values.
  • Interpolation Control: Unlike f-strings, t-strings enable developers to intercept and transform interpolated values, making them safer for use cases like SQL queries and HTML generation.
  • Flexible Processing: Developers can write custom code to process template strings, allowing for advanced string manipulation and domain-specific use cases.
  • Raw Template Strings: t-strings can be combined with the r prefix to create raw template strings, preserving escape sequences.

Motivation

While f-strings are popular, they have limitations in scenarios where developers need to transform interpolated values before combining them into a final string. t-strings address these limitations by providing a mechanism to intercept and process interpolated values, enhancing security and flexibility in string processing.

Examples

Here’s a simple example of using t-strings for HTML generation with automatic sanitization:

evil = "<script>alert('evil')</script>"
template = t"<p>{evil}</p>"
assert html(template) == "<p>&lt;script&gt;alert('evil')&lt;/script&gt;</p>"

For more detailed examples and use cases, refer to the PEP 750 documentation.

Conclusion

t-strings are a significant addition to Python, offering developers a more robust and flexible way to handle string processing. With their ability to intercept and transform interpolated values, t-strings open up new possibilities for safer and more efficient string manipulation in Python.

Sources

PEP 750 – Template Strings (t-strings) have been accepted \ stacker ... PEP 750 – Template Strings (t-strings) have been acceptedpeps.python.org/pep-0750/. 0 sats \ 0 comments \ @hn 6m tech bot. This link was ...
PEP 750 – Template Strings - Python Enhancement Proposals This PEP introduces template strings for custom string processing. Template strings are a generalization of f-strings, using a t in place of the ...
PEP 750 – Template Strings: lazy string formatting evaluation Currently, Python has (among other ways) two different systems that handle {}-based string interpolation: you can make an ordinary string and call its `.format` ...