Skip to content

Commit 0030c80

Browse files
[3.12] GH-109190: Copyedit 3.12 What's New: PEP 701 (GH-109655) (#109689)
(cherry picked from commit e47d12e) Co-authored-by: Adam Turner <[email protected]>
1 parent 5f685ed commit 0030c80

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Doc/whatsnew/3.12.rst

+17-15
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ New Features
154154
PEP 701: Syntactic formalization of f-strings
155155
---------------------------------------------
156156

157-
:pep:`701` lifts some restrictions on the usage of f-strings. Expression components
158-
inside f-strings can now be any valid Python expression including backslashes,
159-
unicode escaped sequences, multi-line expressions, comments and strings reusing the
160-
same quote as the containing f-string. Let's cover these in detail:
157+
:pep:`701` lifts some restrictions on the usage of :term:`f-strings <f-string>`.
158+
Expression components inside f-strings can now be any valid Python expression,
159+
including strings reusing the same quote as the containing f-string,
160+
multi-line expressions, comments, backslashes, and unicode escape sequences.
161+
Let's cover these in detail:
161162

162-
* Quote reuse: in Python 3.11, reusing the same quotes as the containing f-string
163+
* Quote reuse: in Python 3.11, reusing the same quotes as the enclosing f-string
163164
raises a :exc:`SyntaxError`, forcing the user to either use other available
164165
quotes (like using double quotes or triple quotes if the f-string uses single
165166
quotes). In Python 3.12, you can now do things like this:
@@ -182,11 +183,12 @@ same quote as the containing f-string. Let's cover these in detail:
182183
>>> f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"
183184
'2'
184185

185-
* Multi-line expressions and comments: In Python 3.11, f-strings expressions
186-
must be defined in a single line even if outside f-strings expressions could
187-
span multiple lines (like literal lists being defined over multiple lines),
188-
making them harder to read. In Python 3.12 you can now define expressions
189-
spanning multiple lines and include comments on them:
186+
* Multi-line expressions and comments: In Python 3.11, f-string expressions
187+
must be defined in a single line, even if the expression within the f-string
188+
could normally span multiple lines
189+
(like literal lists being defined over multiple lines),
190+
making them harder to read. In Python 3.12 you can now define f-strings
191+
spanning multiple lines, and add inline comments:
190192

191193
>>> f"This is the playlist: {", ".join([
192194
... 'Take me back to Eden', # My, my, those eyes like fire
@@ -196,10 +198,10 @@ same quote as the containing f-string. Let's cover these in detail:
196198
'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'
197199

198200
* Backslashes and unicode characters: before Python 3.12 f-string expressions
199-
couldn't contain any ``\`` character. This also affected unicode escaped
200-
sequences (such as ``\N{snowman}``) as these contain the ``\N`` part that
201-
previously could not be part of expression components of f-strings. Now, you
202-
can define expressions like this:
201+
couldn't contain any ``\`` character. This also affected unicode :ref:`escape
202+
sequences <escape-sequences>` (such as ``\N{snowman}``) as these contain
203+
the ``\N`` part that previously could not be part of expression components of
204+
f-strings. Now, you can define expressions like this:
203205

204206
>>> print(f"This is the playlist: {"\n".join(songs)}")
205207
This is the playlist: Take me back to Eden
@@ -211,7 +213,7 @@ same quote as the containing f-string. Let's cover these in detail:
211213
See :pep:`701` for more details.
212214

213215
As a positive side-effect of how this feature has been implemented (by parsing f-strings
214-
with the PEG parser (see :pep:`617`), now error messages for f-strings are more precise
216+
with :pep:`the PEG parser <617>`, now error messages for f-strings are more precise
215217
and include the exact location of the error. For example, in Python 3.11, the following
216218
f-string raises a :exc:`SyntaxError`:
217219

0 commit comments

Comments
 (0)