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