Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-132661: Document t-strings and templatelib#135229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
5a8dbfa09a1e9eec44c2bd8904b6550aa6da20e0581f30739d935dd61e473627b660be00a535dfcd74e6a796f5d21d337cd0433819a0a301f2e5ca48484b8156ef7035a8cf1b127ebc673e1222530cb6deaec53405b5beb680189a71ddbf41c0ed70ecc86c364c675816a995d7f376d79e87880574c29c6f0f95a95df68fe562c4e25d9a6f72b36fe15dd810fd38fba84dbc1654914fe7eca01e687c506382742779169d15d7491c115eaa504c2e8d2da418b90708405af0434437320dd69d8e0ae6f0726446762File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -462,7 +462,7 @@ Glossary | ||||||||
| core and with user code. | ||||||||
| f-string | ||||||||
| String literals prefixed with ``'f'`` or ``'F'`` are commonly called | ||||||||
| String literals prefixed with ``f`` or ``F`` are commonly called | ||||||||
| "f-strings" which is short for | ||||||||
| :ref:`formatted string literals <f-strings>`. See also :pep:`498`. | ||||||||
| @@ -1322,6 +1322,11 @@ Glossary | ||||||||
| See also :term:`borrowed reference`. | ||||||||
| t-string | ||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
| String literals prefixed with ``t`` or ``T`` are commonly called | ||||||||
| "t-strings" which is short for | ||||||||
| :ref:`template string literals <t-strings>`. | ||||||||
| text encoding | ||||||||
| A string in Python is a sequence of Unicode code points (in range | ||||||||
| ``U+0000``--``U+10FFFF``). To store or transfer a string, it needs to be | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -289,9 +289,9 @@ Literals | ||||||||||||||||||||
| * ``conversion`` is an integer: | ||||||||||||||||||||
| * -1: no formatting | ||||||||||||||||||||
| * 115: ``!s`` string formatting | ||||||||||||||||||||
| * 114: ``!r`` repr formatting | ||||||||||||||||||||
| * 97: ``!a`` ascii formatting | ||||||||||||||||||||
| * 115 (``ord('s')``): ``!s`` string formatting | ||||||||||||||||||||
| * 114 (``ord('r')``): ``!r`` repr formatting | ||||||||||||||||||||
| * 97 (``ord('a')``): ``!a`` ASCII formatting | ||||||||||||||||||||
Comment on lines +292 to +294 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||
| * ``format_spec`` is a :class:`JoinedStr` node representing the formatting | ||||||||||||||||||||
| of the value, or ``None`` if no format was specified. Both | ||||||||||||||||||||
| @@ -325,6 +325,54 @@ Literals | ||||||||||||||||||||
| Constant(value='.3')]))])) | ||||||||||||||||||||
| .. class:: TemplateStr(values) | ||||||||||||||||||||
| A t-string, comprising a series of :class:`Interpolation` and :class:`Constant` | ||||||||||||||||||||
| nodes. | ||||||||||||||||||||
Comment on lines +328 to +331 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a note on AST node ordering Suggested change
| ||||||||||||||||||||
| .. doctest:: | ||||||||||||||||||||
| >>> print(ast.dump(ast.parse('t"{name} finished{place:ordinal}"', mode='eval'), indent=4)) | ||||||||||||||||||||
| Expression( | ||||||||||||||||||||
| body=TemplateStr( | ||||||||||||||||||||
| values=[ | ||||||||||||||||||||
| Interpolation( | ||||||||||||||||||||
| value=Name(id='name'), | ||||||||||||||||||||
| str='name', | ||||||||||||||||||||
| conversion=-1), | ||||||||||||||||||||
| Constant(value=' finished '), | ||||||||||||||||||||
| Interpolation( | ||||||||||||||||||||
| value=Name(id='place'), | ||||||||||||||||||||
| str='place', | ||||||||||||||||||||
| conversion=-1, | ||||||||||||||||||||
| format_spec=JoinedStr( | ||||||||||||||||||||
| values=[ | ||||||||||||||||||||
| Constant(value='ordinal')]))])) | ||||||||||||||||||||
| .. versionadded:: 3.14 | ||||||||||||||||||||
| .. class:: Interpolation(value, str, conversion, format_spec) | ||||||||||||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||
| Node representing a single interpolation field in a t-string. | ||||||||||||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||
| * ``value`` is any expression node (such as a literal, a variable, or a | ||||||||||||||||||||
| function call). | ||||||||||||||||||||
| * ``str`` is a constant containing the text of the interpolation expression. | ||||||||||||||||||||
| * ``conversion`` is an integer: | ||||||||||||||||||||
| * -1: no conversion | ||||||||||||||||||||
| * 115: ``!s`` string conversion | ||||||||||||||||||||
| * 114: ``!r`` repr conversion | ||||||||||||||||||||
| * 97: ``!a`` ascii conversion | ||||||||||||||||||||
AA-Turner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page.
AA-Turner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. Comment on lines +365 to +367 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||
| * ``format_spec`` is a :class:`JoinedStr` node representing the formatting | ||||||||||||||||||||
| of the value, or ``None`` if no format was specified. Both | ||||||||||||||||||||
| ``conversion`` and ``format_spec`` can be set at the same time. | ||||||||||||||||||||
| .. versionadded:: 3.14 | ||||||||||||||||||||
| .. class:: List(elts, ctx) | ||||||||||||||||||||
| Tuple(elts, ctx) | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -2675,9 +2675,9 @@ For example: | ||||||||||||||||
| lead to a number of common errors (such as failing to display tuples and | ||||||||||||||||
| dictionaries correctly). Using the newer :ref:`formatted string literals | ||||||||||||||||
| <f-strings>`, the :meth:`str.format` interface, or :ref:`template strings | ||||||||||||||||
Comment on lines 2676 to 2677 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (part 1). I don't think we need to mention Suggested change
| ||||||||||||||||
| <template-strings>` may help avoid these errors. Each of these | ||||||||||||||||
| alternatives provides their own trade-offs and benefits of simplicity, | ||||||||||||||||
| flexibility, and/or extensibility. | ||||||||||||||||
| ($-strings) <template-strings-pep292>` may help avoid these errors. | ||||||||||||||||
| Each of these alternatives provides their own trade-offs and benefits of | ||||||||||||||||
| simplicity, flexibility, and/or extensibility. | ||||||||||||||||
Comment on lines +2678 to +2680 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (part 2). I don't think we need to mention Suggested change
| ||||||||||||||||
| String objects have one unique built-in operation: the ``%`` operator (modulo). | ||||||||||||||||
| This is also known as the string *formatting* or *interpolation* operator. | ||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -198,8 +198,9 @@ Format String Syntax | ||||||||||||||||||||||||||||||||||
| The :meth:`str.format` method and the :class:`Formatter` class share the same | ||||||||||||||||||||||||||||||||||
| syntax for format strings (although in the case of :class:`Formatter`, | ||||||||||||||||||||||||||||||||||
| subclasses can define their own format string syntax). The syntax is | ||||||||||||||||||||||||||||||||||
| related to that of :ref:`formatted string literals <f-strings>`, but it is | ||||||||||||||||||||||||||||||||||
| less sophisticated and, in particular, does not support arbitrary expressions. | ||||||||||||||||||||||||||||||||||
| related to that of :ref:`formatted string literals <f-strings>` and | ||||||||||||||||||||||||||||||||||
| :ref:`template string literals <t-strings>`, but it is less sophisticated | ||||||||||||||||||||||||||||||||||
| and, in particular, does not support arbitrary expressions. | ||||||||||||||||||||||||||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||||||||||||||||
| .. index:: | ||||||||||||||||||||||||||||||||||
| single:{} (curly brackets); in string formatting | ||||||||||||||||||||||||||||||||||
| @@ -264,6 +265,8 @@ Some simple format string examples:: | ||||||||||||||||||||||||||||||||||
| "Weight in tons{0.weight}" # 'weight' attribute of first positional arg | ||||||||||||||||||||||||||||||||||
| "Units destroyed:{players[0]}" # First element of keyword argument 'players'. | ||||||||||||||||||||||||||||||||||
| .. _formatstrings-conversion: | ||||||||||||||||||||||||||||||||||
| The *conversion* field causes a type coercion before formatting. Normally, the | ||||||||||||||||||||||||||||||||||
| job of formatting a value is done by the :meth:`~object.__format__` method of the value | ||||||||||||||||||||||||||||||||||
| itself. However, in some cases it is desirable to force a type to be formatted | ||||||||||||||||||||||||||||||||||
| @@ -306,7 +309,7 @@ Format Specification Mini-Language | ||||||||||||||||||||||||||||||||||
| "Format specifications" are used within replacement fields contained within a | ||||||||||||||||||||||||||||||||||
| format string to define how individual values are presented (see | ||||||||||||||||||||||||||||||||||
| :ref:`formatstrings`and :ref:`f-strings`). | ||||||||||||||||||||||||||||||||||
| :ref:`formatstrings`, :ref:`f-strings`, and :ref:`t-strings`). | ||||||||||||||||||||||||||||||||||
| They can also be passed directly to the built-in | ||||||||||||||||||||||||||||||||||
| :func:`format` function. Each formattable type may define how the format | ||||||||||||||||||||||||||||||||||
| specification is to be interpreted. | ||||||||||||||||||||||||||||||||||
| @@ -789,10 +792,20 @@ Nesting arguments and more complex examples:: | ||||||||||||||||||||||||||||||||||
| .. _template-strings: | ||||||||||||||||||||||||||||||||||
| .. _template-strings-pep292: | ||||||||||||||||||||||||||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will break anchor references within the page: https://docs.python.org/3/library/string.html#template-strings Are we OK with doing so? | ||||||||||||||||||||||||||||||||||
| Template strings | ||||||||||||||||||||||||||||||||||
| ---------------- | ||||||||||||||||||||||||||||||||||
| Template strings ($-strings) | ||||||||||||||||||||||||||||||||||
| ---------------------------- | ||||||||||||||||||||||||||||||||||
| .. note:: | ||||||||||||||||||||||||||||||||||
| The feature described here was introduced in Python 2.4. It is unrelated | ||||||||||||||||||||||||||||||||||
| to, and should not be confused with, the newer | ||||||||||||||||||||||||||||||||||
| :ref:`template strings <template-strings>` and | ||||||||||||||||||||||||||||||||||
| :ref:`t-string literal syntax <t-strings>` introduced in Python 3.14. | ||||||||||||||||||||||||||||||||||
| T-string literals evaluate to instances of a different | ||||||||||||||||||||||||||||||||||
| :class:`~string.templatelib.Template` class, found in the | ||||||||||||||||||||||||||||||||||
| :mod:`string.templatelib` module. | ||||||||||||||||||||||||||||||||||
Comment on lines +802 to +808 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||||||||||||||||
| Template strings provide simpler string substitutions as described in | ||||||||||||||||||||||||||||||||||
| :pep:`292`. A primary use case for template strings is for | ||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.