summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authoramaajemyfren <32741226+amaajemyfren@users.noreply.github.com>2020-07-27 22:31:02 (GMT)
committerGitHub <noreply@github.com>2020-07-27 22:31:02 (GMT)
commit13efaec2e03288d7ff0ee643589c32bde6c6973c (patch)
tree0ea7b274fd39ad8563fa745fee6609ac30e5e1c9 /Doc/reference
parentb3fbff7289176ba1a322e6899c3d4a04880ed5a7 (diff)
downloadcpython-13efaec2e03288d7ff0ee643589c32bde6c6973c.zip
cpython-13efaec2e03288d7ff0ee643589c32bde6c6973c.tar.gz
cpython-13efaec2e03288d7ff0ee643589c32bde6c6973c.tar.bz2
bpo-41045: Document debug feature of f-strings ('=') (GH-21509)
Co-Authored-By: Rishi <rishi93dev@gmail.com> Automerge-Triggered-By: @gvanrossum
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/lexical_analysis.rst37
1 files changed, 32 insertions, 5 deletions
diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst
index 7d70cbc..4c0f568 100644
--- a/Doc/reference/lexical_analysis.rst
+++ b/Doc/reference/lexical_analysis.rst
@@ -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
@@ -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`
@@ -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.
@@ -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`.
@@ -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