Template Strings in Python

Python has template strings that allow you to process the strings and interpolations before creating the final string:

from string.templatelib import Interpolation

def quote(input: str | Interpolation) -> str:
    if isinstance(input, Interpolation):
        return f"«{input.value}»"
    else:
        return input

cheese = 'Camembert'
cheese_2 = 'Brie'
template = t'Ah! We do have {cheese}, but no {cheese_2}.'
rendered = ''.join(quote(v) for v in template)
# >>> rendered
# 'Ah! We do have «Camembert», but no «Brie».'