Skip to content

Commit d593940

Browse files
committed
Work on the f-string semantics
1 parent 3733e0c commit d593940

File tree

1 file changed

+106
-26
lines changed

1 file changed

+106
-26
lines changed

Doc/reference/lexical_analysis.rst

Lines changed: 106 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -935,58 +935,138 @@ They may contain *replacement fields* delimited by curly braces ``{}``.
935935
Replacement fields contain expressions which are evaluated at run time.
936936
For example::
937937

938-
>>> f'One plus one is {1 + 1}.'
939-
'One plus one is 2.'
938+
>>> who = 'nobody'
939+
>>> nationality = 'Spanish'
940+
>>> f'{who.title()} expects the {nationality} Inquisition!'
941+
'Nobody expects the Spanish Inquisition!'
940942

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::
943+
Any doubled curly braces (``{{`` or ``}}``) outside replacement fields
944+
are replaced with the corresponding single curly brace::
944945

945946
>>> print(f'{{...}}')
946947
{...}
947948

948-
Escape sequences are decoded like in ordinary string literals (except when
949-
a literal is also marked as a raw string)::
949+
Other characters outside replacement fields are treated like in ordinary
950+
string literals.
951+
This means that escape sequences are decoded (except when a literal is
952+
also marked as a raw string), and newlines are possible in triple-quoted
953+
f-strings::
950954

951955
>>> name = 'Galahad'
952956
>>> favorite_color = 'blue'
953957
>>> print(f'{name}:\t{favorite_color}')
954958
Galahad: blue
955-
>>> print(rf'C:\Users\{name}')
959+
>>> print(rf"C:\Users\{name}")
956960
C:\Users\Galahad
961+
>>> print(f'''Three shall be the number of the counting
962+
... and the number of the counting shall be three.''')
963+
Three shall be the number of the counting
964+
and the number of the counting shall be three.
957965

958-
In addition to the expression, replacement fields may contain:
966+
Expressions in formatted string literals are treated like regular
967+
Python expressions.
968+
Each expression is evaluated in the context where the formatted string literal
969+
appears, in order from left to right.
970+
An empty expression is not allowed, and both :keyword:`lambda` and
971+
assignment expressions ``:=`` must be surrounded by explicit parentheses::
972+
973+
>>> f'{(half := 1/2)}, {half * 42}'
974+
'0.5, 21.0'
975+
976+
Replacement expressions can contain newlines in both single-quoted and
977+
triple-quoted f-strings and they can contain comments.
978+
Everything that comes after a ``#`` inside a replacement field
979+
is a comment (even closing braces and quotes).
980+
This means that replacement fields with comments must be closed in a
981+
different line:
982+
983+
.. code-block:: text
984+
985+
>>> a = 2
986+
>>> f"abc{a # This comment }" continues until the end of the line
987+
... + 3}"
988+
'abc5'
989+
990+
After the expression, replacement fields may optionally contain:
959991

960992
* a *debug specifier* -- an equal sign (``=``);
961993
* a *conversion specifier* -- ``!s``, ``!r`` or ``!a``; and/or
962994
* a *format specifier* prefixed with a colon (``:``).
963995

964-
See :ref:`stdtypes-fstrings` for how these specifiers are interpreted.
996+
Debug specifier
997+
^^^^^^^^^^^^^^^
965998

966-
Note that whitespace on both sides of a debug specifier (``=``) is
967-
significant --- it is retained in the result::
999+
If a debug specifier -- an equal sign (``=``) -- appears after the replacement
1000+
field expression, the resulting f-string will contain the expression's source,
1001+
the equal sign, and the value of the expression.
1002+
This is often useful for debugging::
9681003

9691004
>>> print(f'{name=}')
9701005
name='Galahad'
1006+
1007+
Whitespace on both sides of the equal sign is significant --- it is retained
1008+
in the result::
1009+
9711010
>>> print(f'{name = }')
9721011
name = 'Galahad'
9731012

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.
9841013

985-
.. code-block:: text
1014+
Conversion specifier
1015+
^^^^^^^^^^^^^^^^^^^^
9861016

987-
>>> f"abc{a # This is a comment }"
988-
... + 3}"
989-
'abc5'
1017+
By default, the value of a replacement field expression is converted to
1018+
string using :func:`str`::
1019+
1020+
>>> from fractions import Fraction
1021+
>>> one_third = Fraction(1, 3)
1022+
>>> f'{one_third}'
1023+
'1/3'
1024+
1025+
When a debug specifier but no format specifier is used, the default conversion
1026+
instead uses :func:`repr`::
1027+
1028+
>>> f'{one_third = }'
1029+
'one_third = Fraction(1, 3)'
1030+
1031+
The conversion can be specified explicitly using one of these specifiers:
1032+
1033+
* ``!s`` for :func:`str`
1034+
* ``!r`` for :func:`repr`
1035+
* ``!a`` for :func:`ascii`
1036+
1037+
For example::
1038+
1039+
>>> f'{one_third!r} is {one_third!s}'
1040+
'Fraction(1, 3) is 1/3'
1041+
1042+
>>> string = "¡kočka 😸!"
1043+
>>> f'{string = !a}'
1044+
"string = '\\xa1ko\\u010dka \\U0001f638!'"
1045+
1046+
1047+
Format specifier
1048+
^^^^^^^^^^^^^^^^
1049+
1050+
After the expression has been evaluated, and possibly converted using an
1051+
explicit conversion specifier, it is formatted using the :func:`format` function.
1052+
If the replacement field includes a *format specifier*, an arbitrary string
1053+
introduced by a colon (``:``), the specifier is passed to :func:`!format`
1054+
as the second argument.
1055+
The result of :func:`!format` is then used as the final value for the
1056+
replacement field. For example::
1057+
1058+
>>> f'{one_third:.6f}'
1059+
'0.333333'
1060+
>>> f'{one_third:_^+10}'
1061+
'___+1/3___'
1062+
>>> >>> f'{one_third!r:_^20}'
1063+
'___Fraction(1, 3)___'
1064+
>>> f'{one_third = :~>10}~'
1065+
'one_third = ~~~~~~~1/3~'
1066+
1067+
1068+
Formal grammar
1069+
^^^^^^^^^^^^^^
9901070

9911071
.. grammar-snippet:: python-grammar
9921072
:group: python-grammar

0 commit comments

Comments
 (0)