Template strings, or t-strings, are a Python 3.14+ feature that creates Template objects, not regular strings. Unlike f-strings that
immediately evaluate to strings, t-strings require explicit processing by a template processing function to be useful.
When you use a t-string without processing it, you get a Template object that doesn’t have a meaningful string representation. This is
almost always unintentional, as developers typically expect string-like behavior similar to f-strings.
The Template object contains the static string parts and interpolated values separately, allowing for advanced string processing,
security checks, and domain-specific language implementations. However, to get the final string output, you must pass the template to a processing
function.
Using an unprocessed template string can lead to unexpected behavior in your application, such as printing object representations instead of
formatted strings, or passing the wrong type to functions that expect strings.
How to fix?
Process the template string using a template processing function. You can create a simple processing function that mimics f-string behavior, or use
more sophisticated processing for advanced use cases.
Non-compliant code example
name = "World"
template = t"Hello {name}"
print(template) # Noncompliant: prints Template object representation
Compliant code example
name = "World"
template = t"Hello {name}"
print(process_template(template)) # Process the template first
def process_template(template):
from my_lib import html
return html(template)
Documentation