@@ -741,16 +741,28 @@ Expressions in formatted string literals are treated like regular
741
741
Python expressions surrounded by parentheses, with a few exceptions.
742
742
An empty expression is not allowed, and both :keyword: `lambda ` and
743
743
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'
748
756
749
757
.. versionchanged :: 3.7
750
758
Prior to Python 3.7, an :keyword: `await ` expression and comprehensions
751
759
containing an :keyword: `async for ` clause were illegal in the expressions
752
760
in formatted string literals due to a problem with the implementation.
753
761
762
+ .. versionchanged :: 3.12
763
+ Prior to Python 3.12, comments were not allowed inside f-string replacement
764
+ fields.
765
+
754
766
When the equal sign ``'=' `` is provided, the output will have the expression
755
767
text, the ``'=' `` and the evaluated value. Spaces after the opening brace
756
768
``'{' ``, within the expression and after the ``'=' `` are all retained in the
@@ -813,24 +825,30 @@ Some examples of formatted string literals::
813
825
'line = "The mill\'s closed" '
814
826
815
827
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::
819
830
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'
822
834
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.
825
838
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::
827
841
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
830
848
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.
834
852
835
853
Formatted string literals cannot be used as docstrings, even if they do not
836
854
include expressions.
0 commit comments