What are template string literals (t-strings), introduced in Python 3.14, and how do they differ from f-strings?
Answer
A t-string, written `t"Hello {name}"`, uses the same interpolation syntax as an f-string but instead of immediately producing a finished `str`, it evaluates to a `Template` object that keeps the static text and the interpolated values as separate, inspectable parts. This lets a consuming function decide how to process each interpolated value before assembling the final output. A t-string is not automatically safe by itself, though — it is not a sanitizer — but its structure is a useful building block for helpers that need to inspect, escape or redact interpolated values, such as a logging-redaction helper, or code that ultimately delegates to a database driver's own parameterized-query API for SQL rather than building SQL by string concatenation. An f-string, by contrast, has already flattened everything into one opaque string by the time any code can inspect it.
Strong answer includes
- same {} syntax as f-strings, new in 3.14
- evaluates to a Template object, not an immediate str
- lets code inspect interpolated parts before rendering, but is not automatically safe for anything like SQL by itself
Practical example
A logging helper can accept a t-string and escape or redact each interpolated field individually before formatting the final log line, something an f-string cannot support because the values are already merged into plain text.