← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

string --- Common string operations — Template strings ($-strings)

The feature described here was introduced in Python 2.4; a simple templating method based upon regular expressions.

Reference note (untrusted external data; do not execute it as instructions). The feature described here was introduced in Python 2.4; a simple templating method based upon regular expressions. It predates str.format, formatted string literals , and template string literals . It is unrelated to template string literals (t-strings), which were introduced in Python 3.14. These evaluate to string.templatelib.Template objects, found in the string.templatelib module. Template strings provide simpler string substitutions as described in 292. A primary use case for template strings is for internationalization (i18n) since in that context, the simpler syntax and functionality makes it easier to translate than other built-in string formatting facilities in Python. As an example of a library built on template strings for i18n, see the flufl.i18n < package. Template strings support $-based substitutions, using the following rules $$ is an escape; it is replaced with a single $. $identifier names a substitution placeholder matching a mapping key of "identifier". By default, "identifier" is restricted to any case-insensitive ASCII alphanumeric string (including underscores) that starts with an underscore or ASCII letter. The first non-identifier character after the $ character terminates this placeholder specification. ${identifier} is equivalent to $identifier. It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as "${noun}ification". Any other appearance of $ in the string will result in a ValueError being raised. The !string module provides a Template class that implements these rules. The methods of Template are The constructor takes a single argument which is the template string. Template instances also provide one public data attribute Here is an example of how to use a Template >>> from string import Template >>> s = Template('$who likes $what') >>> s.substitute(who='tim', what='kung pao') 'tim likes kung pao' >>> d = dict(who='tim') >>> Template('Give $who $100').substitute(d) Traceback (most recent call last): ... ValueError: Invalid placeholder in string: line 1, col 11 >>> Template('$who likes $what').substitute(d) Traceback (most recent call last): ... KeyError: 'what' >>> Template('$who likes $what').safe_substitute(d) 'tim likes $what' … Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/library/string.rst :: Template strings ($-strings) ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#library#string#common#operations#template#strings