@@ -153,12 +153,13 @@ New Features
153
153
PEP 701: Syntactic formalization of f-strings
154
154
---------------------------------------------
155
155
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:
160
161
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
162
163
raises a :exc: `SyntaxError `, forcing the user to either use other available
163
164
quotes (like using double quotes or triple quotes if the f-string uses single
164
165
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:
181
182
>>> f " { f " { f " { f " { f " { f " { 1 + 1 } " } " } " } " } " } "
182
183
'2'
183
184
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:
189
191
190
192
>>> f " This is the playlist: { " , " .join([
191
193
... ' 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:
195
197
'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'
196
198
197
199
* 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:
202
204
203
205
>>> print (f " This is the playlist: { " \n " .join(songs)} " )
204
206
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:
210
212
See :pep: `701 ` for more details.
211
213
212
214
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
214
216
and include the exact location of the error. For example, in Python 3.11, the following
215
217
f-string raises a :exc: `SyntaxError `:
216
218
0 commit comments