Skip to content

Commit e47d12e

Browse files
authored
GH-109190: Copyedit 3.12 What's New: PEP 701 (#109655)
1 parent 16c2402 commit e47d12e

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
@@ -153,12 +153,13 @@ New Features
153153
PEP 701: Syntactic formalization of f-strings
154154
---------------------------------------------
155155

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)