A template string, or t-string, lets you combine the template with a function that operates on the template’s structure, not just its output. You could write a template handler that allows all variables placed in the template, or only variables of a specific type, or only variables that match some output, to be manipulated at output time. You could also handle the variables and the interpolating text as separate, differently typed objects.
For instance, if you have the template t"My name is {user_name}, and I'm from {user_locale}"
, you could have the variables user_name
and user_locale
automatically cleaned of any HTML before display. You could also perform transformations on the My name is
and and I'm from
portions of the output automatically, as those would be tagged with the special type Interpolation
.
Template strings will make it far easier to write template engines, e.g., Jinja2, or to duplicate much of the functionality of those template engines directly in Python without the overhead of third-party libraries.