Skip to content

Commit e7b57b5

Browse files
committed
Continue with f-strings
1 parent 1e0c84a commit e7b57b5

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

Doc/library/stdtypes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,8 @@ expression support in the :mod:`re` module).
25262526
single: : (colon); in formatted string literal
25272527
single: = (equals); for help in debugging using string literals
25282528

2529+
.. _stdtypes-fstrings:
2530+
25292531
Formatted String Literals (f-strings)
25302532
-------------------------------------
25312533

Doc/reference/lexical_analysis.rst

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -931,17 +931,62 @@ f-strings
931931
A :dfn:`formatted string literal` or :dfn:`f-string` is a string literal
932932
that is prefixed with ``'f'`` or ``'F'``.
933933
Unlike other string literals, f-strings do not have a constant value.
934-
They may contain *replacement fields*, which are expressions delimited by
935-
curly braces ``{}``, which are evaluated at run time.
934+
They may contain *replacement fields* delimited by curly braces ``{}``.
935+
Replacement fields contain expressions which are evaluated at run time.
936936
For example::
937937

938938
>>> f'One plus one is {1 + 1}.'
939939
'One plus one is 2.'
940940

941+
The parts of the string outside curly braces are treated literally,
942+
except that any doubled curly braces ``'{{'`` or ``'}}'`` are replaced
943+
with the corresponding single curly brace::
944+
945+
>>> print(f'{{...}}')
946+
{...}
941947

942948
Escape sequences are decoded like in ordinary string literals (except when
943-
a literal is also marked as a raw string). After decoding, the grammar
944-
for the contents of the string is:
949+
a literal is also marked as a raw string)::
950+
951+
>>> name = 'Galahad'
952+
>>> favorite_color = 'blue'
953+
>>> print(f'{name}:\t{favorite_color}')
954+
Galahad: blue
955+
>>> print(rf'C:\Users\{name}')
956+
C:\Users\Galahad
957+
958+
In addition to the expression, replacement fields may contain:
959+
960+
* a *debug specifier* -- an equal sign (``=``);
961+
* a *conversion specifier* -- ``!s``, ``!r`` or ``!a``; and/or
962+
* a *format specifier* prefixed with a colon (``:``).
963+
964+
See :ref:`stdtypes-fstrings` for how these specifiers are interpreted.
965+
966+
Note that whitespace on both sides of a debug specifier (``=``) is
967+
significant --- it is retained in the result::
968+
969+
>>> print(f'{name=}')
970+
name='Galahad'
971+
>>> print(f'{name = }')
972+
name = 'Galahad'
973+
974+
Expressions in formatted string literals are treated like regular
975+
Python expressions surrounded by parentheses, with a few exceptions.
976+
An empty expression is not allowed, and both :keyword:`lambda` and
977+
assignment expressions ``:=`` must be surrounded by explicit parentheses.
978+
Each expression is evaluated in the context where the formatted string literal
979+
appears, in order from left to right. Replacement expressions can contain
980+
newlines in both single-quoted and triple-quoted f-strings and they can contain
981+
comments. Everything that comes after a ``#`` inside a replacement field
982+
is a comment (even closing braces and quotes). In that case, replacement fields
983+
must be closed in a different line.
984+
985+
.. code-block:: text
986+
987+
>>> f"abc{a # This is a comment }"
988+
... + 3}"
989+
'abc5'
945990
946991
.. grammar-snippet:: python-grammar
947992
:group: python-grammar
@@ -979,38 +1024,6 @@ for the contents of the string is:
9791024
| `yield_expression`
9801025
9811026

982-
---------------
983-
984-
985-
986-
987-
The parts of the string outside curly braces are treated literally,
988-
except that any doubled curly braces ``'{{'`` or ``'}}'`` are replaced
989-
with the corresponding single curly brace. A single opening curly
990-
bracket ``'{'`` marks a replacement field, which starts with a
991-
Python expression. To display both the expression text and its value after
992-
evaluation, (useful in debugging), an equal sign ``'='`` may be added after the
993-
expression. A conversion field, introduced by an exclamation point ``'!'`` may
994-
follow. A format specifier may also be appended, introduced by a colon ``':'``.
995-
A replacement field ends with a closing curly bracket ``'}'``.
996-
997-
Expressions in formatted string literals are treated like regular
998-
Python expressions surrounded by parentheses, with a few exceptions.
999-
An empty expression is not allowed, and both :keyword:`lambda` and
1000-
assignment expressions ``:=`` must be surrounded by explicit parentheses.
1001-
Each expression is evaluated in the context where the formatted string literal
1002-
appears, in order from left to right. Replacement expressions can contain
1003-
newlines in both single-quoted and triple-quoted f-strings and they can contain
1004-
comments. Everything that comes after a ``#`` inside a replacement field
1005-
is a comment (even closing braces and quotes). In that case, replacement fields
1006-
must be closed in a different line.
1007-
1008-
.. code-block:: text
1009-
1010-
>>> f"abc{a # This is a comment }"
1011-
... + 3}"
1012-
'abc5'
1013-
10141027
.. versionchanged:: 3.7
10151028
Prior to Python 3.7, an :keyword:`await` expression and comprehensions
10161029
containing an :keyword:`async for` clause were illegal in the expressions
@@ -1020,6 +1033,11 @@ must be closed in a different line.
10201033
Prior to Python 3.12, comments were not allowed inside f-string replacement
10211034
fields.
10221035

1036+
---------------
1037+
1038+
1039+
1040+
10231041
When the equal sign ``'='`` is provided, the output will have the expression
10241042
text, the ``'='`` and the evaluated value. Spaces after the opening brace
10251043
``'{'``, within the expression and after the ``'='`` are all retained in the

0 commit comments

Comments
 (0)