Skip to content

Commit 8e5b3b9

Browse files
authored
gh-102856: Update "Formatted string literals" docs section after PEP701 (#104861)
1 parent fc07fe4 commit 8e5b3b9

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

Doc/reference/lexical_analysis.rst

+35-17
Original file line numberDiff line numberDiff line change
@@ -741,16 +741,28 @@ Expressions in formatted string literals are treated like regular
741741
Python expressions surrounded by parentheses, with a few exceptions.
742742
An empty expression is not allowed, and both :keyword:`lambda` and
743743
assignment expressions ``:=`` must be surrounded by explicit parentheses.
744-
Replacement expressions can contain line breaks (e.g. in triple-quoted
745-
strings), but they cannot contain comments. Each expression is evaluated
746-
in the context where the formatted string literal appears, in order from
747-
left to right.
744+
Each expression is evaluated in the context where the formatted string literal
745+
appears, in order from left to right. Replacement expressions can contain
746+
newlines in both single-quoted and triple-quoted f-strings and they can contain
747+
comments. Everything that comes after a ``#`` inside a replacement field
748+
is a comment (even closing braces and quotes). In that case, replacement fields
749+
must be closed in a different line.
750+
751+
.. code-block:: text
752+
753+
>>> f"abc{a # This is a comment }"
754+
... + 3}"
755+
'abc5'
748756
749757
.. versionchanged:: 3.7
750758
Prior to Python 3.7, an :keyword:`await` expression and comprehensions
751759
containing an :keyword:`async for` clause were illegal in the expressions
752760
in formatted string literals due to a problem with the implementation.
753761

762+
.. versionchanged:: 3.12
763+
Prior to Python 3.12, comments were not allowed inside f-string replacement
764+
fields.
765+
754766
When the equal sign ``'='`` is provided, the output will have the expression
755767
text, the ``'='`` and the evaluated value. Spaces after the opening brace
756768
``'{'``, within the expression and after the ``'='`` are all retained in the
@@ -813,24 +825,30 @@ Some examples of formatted string literals::
813825
'line = "The mill\'s closed" '
814826

815827

816-
A consequence of sharing the same syntax as regular string literals is
817-
that characters in the replacement fields must not conflict with the
818-
quoting used in the outer formatted string literal::
828+
Reusing the outer f-string quoting type inside a replacement field is
829+
permitted::
819830

820-
f"abc {a["x"]} def" # error: outer string literal ended prematurely
821-
f"abc {a['x']} def" # workaround: use different quoting
831+
>>> a = dict(x=2)
832+
>>> f"abc {a["x"]} def"
833+
'abc 2 def'
822834

823-
Backslashes are not allowed in format expressions and will raise
824-
an error::
835+
.. versionchanged:: 3.12
836+
Prior to Python 3.12, reuse of the same quoting type of the outer f-string
837+
inside a replacement field was not possible.
825838

826-
f"newline: {ord('\n')}" # raises SyntaxError
839+
Backslashes are also allowed in replacement fields and are evaluated the same
840+
way as in any other context::
827841

828-
To include a value in which a backslash escape is required, create
829-
a temporary variable.
842+
>>> a = ["a", "b", "c"]
843+
>>> print(f"List a contains:\n{"\n".join(a)}")
844+
List a contains:
845+
a
846+
b
847+
c
830848

831-
>>> newline = ord('\n')
832-
>>> f"newline: {newline}"
833-
'newline: 10'
849+
.. versionchanged:: 3.12
850+
Prior to Python 3.12, backslashes were not permitted inside an f-string
851+
replacement field.
834852

835853
Formatted string literals cannot be used as docstrings, even if they do not
836854
include expressions.

0 commit comments

Comments
 (0)