Skip to content

Commit 9f74deb

Browse files
authored
Improve the documentation for template strings (#856)
bpo-19824 bpo-20314 bpo-12518
1 parent 8cea592 commit 9f74deb

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

Doc/library/stdtypes.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,10 +2076,11 @@ expression support in the :mod:`re` module).
20762076

20772077
The formatting operations described here exhibit a variety of quirks that
20782078
lead to a number of common errors (such as failing to display tuples and
2079-
dictionaries correctly). Using the newer :ref:`formatted
2080-
string literals <f-strings>` or the :meth:`str.format` interface
2081-
helps avoid these errors. These alternatives also provide more powerful,
2082-
flexible and extensible approaches to formatting text.
2079+
dictionaries correctly). Using the newer :ref:`formatted string literals
2080+
<f-strings>`, the :meth:`str.format` interface, or :ref:`template strings
2081+
<template-strings>` may help avoid these errors. Each of these
2082+
alternatives provides their own trade-offs and benefits of simplicity,
2083+
flexibility, and/or extensibility.
20832084

20842085
String objects have one unique built-in operation: the ``%`` operator (modulo).
20852086
This is also known as the string *formatting* or *interpolation* operator.

Doc/library/string.rst

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,15 @@ Nesting arguments and more complex examples::
657657
Template strings
658658
----------------
659659

660-
Templates provide simpler string substitutions as described in :pep:`292`.
661-
Instead of the normal ``%``\ -based substitutions, Templates support ``$``\
662-
-based substitutions, using the following rules:
660+
Template strings provide simpler string substitutions as described in
661+
:pep:`292`. A primary use case for template strings is for
662+
internationalization (i18n) since in that context, the simpler syntax and
663+
functionality makes it easier to translate than other built-in string
664+
formatting facilities in Python. As an example of a library built on template
665+
strings for i18n, see the
666+
`flufl.i18n <http://flufli18n.readthedocs.io/en/latest/>`_ package.
667+
668+
Template strings support ``$``-based substitutions, using the following rules:
663669

664670
* ``$$`` is an escape; it is replaced with a single ``$``.
665671

@@ -735,14 +741,17 @@ Here is an example of how to use a Template::
735741
>>> Template('$who likes $what').safe_substitute(d)
736742
'tim likes $what'
737743

738-
Advanced usage: you can derive subclasses of :class:`Template` to customize the
739-
placeholder syntax, delimiter character, or the entire regular expression used
740-
to parse template strings. To do this, you can override these class attributes:
744+
Advanced usage: you can derive subclasses of :class:`Template` to customize
745+
the placeholder syntax, delimiter character, or the entire regular expression
746+
used to parse template strings. To do this, you can override these class
747+
attributes:
741748

742-
* *delimiter* -- This is the literal string describing a placeholder introducing
743-
delimiter. The default value is ``$``. Note that this should *not* be a
744-
regular expression, as the implementation will call :meth:`re.escape` on this
745-
string as needed.
749+
* *delimiter* -- This is the literal string describing a placeholder
750+
introducing delimiter. The default value is ``$``. Note that this should
751+
*not* be a regular expression, as the implementation will call
752+
:meth:`re.escape` on this string as needed. Note further that you cannot
753+
change the delimiter after class creation (i.e. a different delimiter must
754+
be set in the subclass's class namespace).
746755

747756
* *idpattern* -- This is the regular expression describing the pattern for
748757
non-braced placeholders (the braces will be added automatically as
@@ -787,4 +796,3 @@ Helper functions
787796
or ``None``, runs of whitespace characters are replaced by a single space
788797
and leading and trailing whitespace are removed, otherwise *sep* is used to
789798
split and join the words.
790-

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,10 @@ C API
879879
Documentation
880880
-------------
881881

882+
- bpo-19824, bpo-20314, bpo-12518: Improve the documentation for, and links
883+
to, template strings by emphasizing their utility for internationalization,
884+
and by clarifying some usage constraints.
885+
882886
- bpo-28929: Link the documentation to its source file on GitHub.
883887

884888
- bpo-25008: Document smtpd.py as effectively deprecated and add a pointer to

0 commit comments

Comments
 (0)