File Doc/library/stdtypes.rst changed (mode: 100644) (index 9cf876783e..a9877ba666) |
... |
... |
expression support in the :mod:`re` module). |
2076 |
2076 |
|
|
2077 |
2077 |
The formatting operations described here exhibit a variety of quirks that |
The formatting operations described here exhibit a variety of quirks that |
2078 |
2078 |
lead to a number of common errors (such as failing to display tuples and |
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. |
2083 |
2084 |
|
|
2084 |
2085 |
String objects have one unique built-in operation: the ``%`` operator (modulo). |
String objects have one unique built-in operation: the ``%`` operator (modulo). |
2085 |
2086 |
This is also known as the string *formatting* or *interpolation* operator. |
This is also known as the string *formatting* or *interpolation* operator. |
File Doc/library/string.rst changed (mode: 100644) (index 03eaf3b9cd..8176a81d4c) |
... |
... |
Nesting arguments and more complex examples:: |
657 |
657 |
Template strings |
Template strings |
658 |
658 |
---------------- |
---------------- |
659 |
659 |
|
|
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: |
663 |
669 |
|
|
664 |
670 |
* ``$$`` is an escape; it is replaced with a single ``$``. |
* ``$$`` is an escape; it is replaced with a single ``$``. |
665 |
671 |
|
|
|
... |
... |
Here is an example of how to use a Template:: |
735 |
741 |
>>> Template('$who likes $what').safe_substitute(d) |
>>> Template('$who likes $what').safe_substitute(d) |
736 |
742 |
'tim likes $what' |
'tim likes $what' |
737 |
743 |
|
|
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: |
741 |
748 |
|
|
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). |
746 |
755 |
|
|
747 |
756 |
* *idpattern* -- This is the regular expression describing the pattern for |
* *idpattern* -- This is the regular expression describing the pattern for |
748 |
757 |
non-braced placeholders (the braces will be added automatically as |
non-braced placeholders (the braces will be added automatically as |
|
... |
... |
Helper functions |
787 |
796 |
or ``None``, runs of whitespace characters are replaced by a single space |
or ``None``, runs of whitespace characters are replaced by a single space |
788 |
797 |
and leading and trailing whitespace are removed, otherwise *sep* is used to |
and leading and trailing whitespace are removed, otherwise *sep* is used to |
789 |
798 |
split and join the words. |
split and join the words. |
790 |
|
|
|
File Misc/NEWS changed (mode: 100644) (index b41fd8d79f..9ff9481fd1) |
... |
... |
C API |
879 |
879 |
Documentation |
Documentation |
880 |
880 |
------------- |
------------- |
881 |
881 |
|
|
|
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 |
|
|
882 |
886 |
- bpo-28929: Link the documentation to its source file on GitHub. |
- bpo-28929: Link the documentation to its source file on GitHub. |
883 |
887 |
|
|
884 |
888 |
- bpo-25008: Document smtpd.py as effectively deprecated and add a pointer to |
- bpo-25008: Document smtpd.py as effectively deprecated and add a pointer to |