mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
[3.12] gh-102856: Update "Formatted string literals" docs section after PEP701 (GH-104861) (#104865)
(cherry picked from commit 8e5b3b90c8
)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
This commit is contained in:
parent
5e1799ea2e
commit
25890ebbb8
1 changed files with 35 additions and 17 deletions
|
@ -741,16 +741,28 @@ Expressions in formatted string literals are treated like regular
|
||||||
Python expressions surrounded by parentheses, with a few exceptions.
|
Python expressions surrounded by parentheses, with a few exceptions.
|
||||||
An empty expression is not allowed, and both :keyword:`lambda` and
|
An empty expression is not allowed, and both :keyword:`lambda` and
|
||||||
assignment expressions ``:=`` must be surrounded by explicit parentheses.
|
assignment expressions ``:=`` must be surrounded by explicit parentheses.
|
||||||
Replacement expressions can contain line breaks (e.g. in triple-quoted
|
Each expression is evaluated in the context where the formatted string literal
|
||||||
strings), but they cannot contain comments. Each expression is evaluated
|
appears, in order from left to right. Replacement expressions can contain
|
||||||
in the context where the formatted string literal appears, in order from
|
newlines in both single-quoted and triple-quoted f-strings and they can contain
|
||||||
left to right.
|
comments. Everything that comes after a ``#`` inside a replacement field
|
||||||
|
is a comment (even closing braces and quotes). In that case, replacement fields
|
||||||
|
must be closed in a different line.
|
||||||
|
|
||||||
|
.. code-block:: text
|
||||||
|
|
||||||
|
>>> f"abc{a # This is a comment }"
|
||||||
|
... + 3}"
|
||||||
|
'abc5'
|
||||||
|
|
||||||
.. versionchanged:: 3.7
|
.. versionchanged:: 3.7
|
||||||
Prior to Python 3.7, an :keyword:`await` expression and comprehensions
|
Prior to Python 3.7, an :keyword:`await` expression and comprehensions
|
||||||
containing an :keyword:`async for` clause were illegal in the expressions
|
containing an :keyword:`async for` clause were illegal in the expressions
|
||||||
in formatted string literals due to a problem with the implementation.
|
in formatted string literals due to a problem with the implementation.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.12
|
||||||
|
Prior to Python 3.12, comments were not allowed inside f-string replacement
|
||||||
|
fields.
|
||||||
|
|
||||||
When the equal sign ``'='`` is provided, the output will have the expression
|
When the equal sign ``'='`` is provided, the output will have the expression
|
||||||
text, the ``'='`` and the evaluated value. Spaces after the opening brace
|
text, the ``'='`` and the evaluated value. Spaces after the opening brace
|
||||||
``'{'``, within the expression and after the ``'='`` are all retained in the
|
``'{'``, within the expression and after the ``'='`` are all retained in the
|
||||||
|
@ -813,24 +825,30 @@ Some examples of formatted string literals::
|
||||||
'line = "The mill\'s closed" '
|
'line = "The mill\'s closed" '
|
||||||
|
|
||||||
|
|
||||||
A consequence of sharing the same syntax as regular string literals is
|
Reusing the outer f-string quoting type inside a replacement field is
|
||||||
that characters in the replacement fields must not conflict with the
|
permitted::
|
||||||
quoting used in the outer formatted string literal::
|
|
||||||
|
|
||||||
f"abc {a["x"]} def" # error: outer string literal ended prematurely
|
>>> a = dict(x=2)
|
||||||
f"abc {a['x']} def" # workaround: use different quoting
|
>>> f"abc {a["x"]} def"
|
||||||
|
'abc 2 def'
|
||||||
|
|
||||||
Backslashes are not allowed in format expressions and will raise
|
.. versionchanged:: 3.12
|
||||||
an error::
|
Prior to Python 3.12, reuse of the same quoting type of the outer f-string
|
||||||
|
inside a replacement field was not possible.
|
||||||
|
|
||||||
f"newline: {ord('\n')}" # raises SyntaxError
|
Backslashes are also allowed in replacement fields and are evaluated the same
|
||||||
|
way as in any other context::
|
||||||
|
|
||||||
To include a value in which a backslash escape is required, create
|
>>> a = ["a", "b", "c"]
|
||||||
a temporary variable.
|
>>> print(f"List a contains:\n{"\n".join(a)}")
|
||||||
|
List a contains:
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
|
||||||
>>> newline = ord('\n')
|
.. versionchanged:: 3.12
|
||||||
>>> f"newline: {newline}"
|
Prior to Python 3.12, backslashes were not permitted inside an f-string
|
||||||
'newline: 10'
|
replacement field.
|
||||||
|
|
||||||
Formatted string literals cannot be used as docstrings, even if they do not
|
Formatted string literals cannot be used as docstrings, even if they do not
|
||||||
include expressions.
|
include expressions.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue