This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:pep:`701` lifts some restrictions on the usage of f-strings. Expression components
inside f-strings can now be any valid Python expression including backslashes,
unicode escaped sequences, multi-line expressions, comments and strings reusing the
same quote as the containing f-string. Let's cover these in detail:
:pep:`701` lifts some restrictions on the usage of :term:`f-strings <f-string>`.
Expression components inside f-strings can now be any valid Python expression,
including strings reusing the same quote as the containing f-string,
multi-line expressions, comments, backslashes, and unicode escape sequences.
Let's cover these in detail:
* Quote reuse: in Python 3.11, reusing the same quotes as the containing f-string
* Quote reuse: in Python 3.11, reusing the same quotes as the enclosing f-string
raises a :exc:`SyntaxError`, forcing the user to either use other available
quotes (like using double quotes or triple quotes if the f-string uses single
quotes). In Python 3.12, you can now do things like this:
Expand All
@@ -183,11 +184,12 @@ same quote as the containing f-string. Let's cover these in detail:
>>> f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"
'2'
* Multi-line expressions and comments: In Python 3.11, f-strings expressions
must be defined in a single line even if outside f-strings expressions could
span multiple lines (like literal lists being defined over multiple lines),
making them harder to read. In Python 3.12 you can now define expressions
spanning multiple lines and include comments on them:
* Multi-line expressions and comments: In Python 3.11, f-string expressions
must be defined in a single line, even if the expression within the f-string
could normally span multiple lines
(like literal lists being defined over multiple lines),
making them harder to read. In Python 3.12 you can now define f-strings
spanning multiple lines, and add inline comments:
>>> f"This is the playlist: {", ".join([
... 'Take me back to Eden', # My, my, those eyes like fire
Expand All
@@ -197,10 +199,10 @@ same quote as the containing f-string. Let's cover these in detail:
'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'
* Backslashes and unicode characters: before Python 3.12 f-string expressions
couldn't contain any ``\`` character. This also affected unicode escaped
sequences (such as ``\N{snowman}``) as these contain the ``\N`` part that
previously could not be part of expression components of f-strings. Now, you
can define expressions like this:
couldn't contain any ``\`` character. This also affected unicode :ref:`escape
sequences <escape-sequences>` (such as ``\N{snowman}``) as these contain
the ``\N`` part that previously could not be part of expression components of
f-strings. Now, you can define expressions like this:
>>> print(f"This is the playlist: {"\n".join(songs)}")
This is the playlist: Take me back to Eden
Expand All
@@ -212,7 +214,7 @@ same quote as the containing f-string. Let's cover these in detail:
See :pep:`701` for more details.
As a positive side-effect of how this feature has been implemented (by parsing f-strings
with the PEG parser (see :pep:`617`), now error messages for f-strings are more precise
with :pep:`the PEG parser <617>`, now error messages for f-strings are more precise
and include the exact location of the error. For example, in Python 3.11, the following
f-string raises a :exc:`SyntaxError`:
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[3.12] GH-109190: Copyedit 3.12 What's New: PEP 701 (GH-109655) #109689
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
[3.12] GH-109190: Copyedit 3.12 What's New: PEP 701 (GH-109655) #109689
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing