Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions Doc/reference/lexical_analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,11 @@ and formatted string literals may be concatenated with plain string literals.
single: string; formatted literal
single: string; interpolated literal
single: f-string
single: fstring
single: {} (curly brackets); in formatted string literal
single: ! (exclamation); in formatted string literal
single: : (colon); in formatted string literal
single: = (equals); for help in debugging using string literals
.. _f-strings:

Formatted string literals
Expand All @@ -659,7 +661,7 @@ for the contents of the string is:

.. productionlist::
f_string: (`literal_char` | "{{" | "}}" | `replacement_field`)*
replacement_field: "{" `f_expression` ["!" `conversion`] [":" `format_spec`] "}"
replacement_field: "{" `f_expression` ["="] ["!" `conversion`] [":" `format_spec`] "}"
f_expression: (`conditional_expression` | "*" `or_expr`)
: ("," `conditional_expression` | "," "*" `or_expr`)* [","]
: | `yield_expression`
Expand All @@ -671,10 +673,11 @@ The parts of the string outside curly braces are treated literally,
except that any doubled curly braces ``'{{'`` or ``'}}'`` are replaced
with the corresponding single curly brace. A single opening curly
bracket ``'{'`` marks a replacement field, which starts with a
Python expression. After the expression, there may be a conversion field,
introduced by an exclamation point ``'!'``. A format specifier may also
be appended, introduced by a colon ``':'``. A replacement field ends
with a closing curly bracket ``'}'``.
Python expression. To display both the expression text and its value after
evaluation, (useful in debugging), an equal sign ``'='`` may be added after the
expression. A conversion field, introduced by an exclamation point ``'!'`` may
follow. A format specifier may also be appended, introduced by a colon ``':'``.
A replacement field ends with a closing curly bracket ``'}'``.

Expressions in formatted string literals are treated like regular
Python expressions surrounded by parentheses, with a few exceptions.
Expand All @@ -690,6 +693,17 @@ left to right.
containing an :keyword:`async for` clause were illegal in the expressions
in formatted string literals due to a problem with the implementation.

When the equal sign ``'='`` is provided, the output will have the expression
text, the ``'='`` and the evaluated value. Spaces after the opening brace
``'{'``, within the expression and after the ``'='`` are all retained in the
output. By default, the ``'='`` causes the :func:`repr` of the expression to be
provided, unless there is a format specified. When a format is specified it
defaults to the :func:`str` of the expression unless a conversion ``'!r'`` is
declared.

.. versionadded:: 3.8
The equal sign ``'='`` was added in Python 3.8.

If a conversion is specified, the result of evaluating the expression
is converted before formatting. Conversion ``'!s'`` calls :func:`str` on
the result, ``'!r'`` calls :func:`repr`, and ``'!a'`` calls :func:`ascii`.
Expand Down Expand Up @@ -724,9 +738,22 @@ Some examples of formatted string literals::
>>> today = datetime(year=2017, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
'January 27, 2017'
>>> f"{today=:%B %d, %Y}" # using date format specifier and debugging
'today=January 27, 2017'
>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
'0x400'
>>> foo = "bar"
>>> f"{ foo = }" # preserves whitespace
" foo = 'bar'"
>>> line = "The mill's closed"
>>> f"{line = }"
'line = "The mill\'s closed"'
>>> f"{line = :20}"
"line = The mill's closed "
>>> f"{line = !r:20}"
'line = "The mill\'s closed" '


A consequence of sharing the same syntax as regular string literals is
that characters in the replacement fields must not conflict with the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add documentation for debug feature of f-strings.