Python  

Structured Strings in Python 3.14: A Deep Dive into Template Literals

In this article, we explore template string literals (t-strings) introduced in Python 3.14 and how they enable safer and more flexible string processing. Template strings look similar to f-strings but behave very differently. While f-strings immediately evaluate to a str, t-strings produce a Template object. This object preserves a structured representation of the string by separating static text and interpolated expressions. This separation allows developers to inspect, transform, or validate interpolated values before producing the final string.

At this point, no string interpolation occurs. Instead, Python creates a Template instance containing ordered parts of the string. A Template object is iterable. Each iteration yields either a plain string segment, or an Interpolation object representing a dynamic value. This design allows fine-grained control over how interpolated values are handled. Because templates distinguish static text from user-provided data at runtime, they are particularly useful for several use cases.

Practical Use Case

  • Content Filtering / Sanitization

  • Input validation

  • Content filtering

  • Escaping untrusted values (e.g., HTML, SQL, logs)

To create a template string, use the t prefix instead of f:

  
    template_sample = t'Hi {input}' # template string
 normal_sample = f'Hi {input}' #normal interpolated string
  

Before this concept there was poor scalability for complex strings that would have required multiple interpolations resulting to poor code readability.

There are other potential ways too to create a reusable static object that could potentially solve this problem but was not standardized.

Below is one example that uses normal interpolation:

  
    def blacklisted_words():
    return ['Hate','Violence']

def sanitize(value: str):
    blacklisted_words_list = blacklisted_words()
    if value in blacklisted_words_list:
        return "<omitted>"
    return value

input = "Hate"
name = "Varun"
result = f"Hi {sanitize(input)} I am {{sanitize(name)}}"
print(result) #Output Hi <omitted> I am Varun
  

Now, with new template literals we can write reusable code. Below is one example that improves code readability compared to above.

  
    from string.templatelib import Interpolation

def blacklisted_words():
    return ['Hate','Violence']

input = 'Hate'
name = 'Varun'
blacklistedval_template_sample = t'Hi {input} I am {name}'


def get_generated_template_string(template_str: Interpolation):
    parts = []
    blacklisted_words_list = blacklisted_words()
    for word_template in template_str:
        if isinstance(word_template, Interpolation):
            if word_template.value not in blacklisted_words_list:
                parts.append(str(word_template.value))
            else:
                parts.append("<omitted>")
        else:
            parts.append(word_template)

    return ''.join(parts)

g_str = get_generated_template_string(blacklistedval_template_sample)
print(g_str) #Hi <omitted> I am Varun
  

That's it!!

Thanks for reading till the end. I hope you find this article insightful. Let me know in comments how you feel about this new concept.