8000 gh-85583: Document f-strings in library/stdtypes.rst by amaajemyfren · Pull Request #21552 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-85583: Document f-strings in library/stdtypes.rst #21552

New issue

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

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
bpo-41411 Improve and consolidate f-string
Clean up of small errors.
  • Loading branch information
amaajemyfren committed Jul 28, 2020
commit a37a88839beeda3158c0fcaaaa2bfcc67284a81c
21 changes: 14 additions & 7 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ The new way to do this is to use the equal sign ``=`` after the expression.


When used by its own, the debugging operator ``=``, outputs the :func:`repr` of
the expression. A change this a converter is used::
the expression. A converter can be used to change it::

>>> f"{now=!s}"
'now=2020-07-28 04:33:08.629606'
Expand All @@ -2512,13 +2512,13 @@ expression or conversion result. An empty string is passed when the format
specifier is omitted. The formatted result is then returned as the final value
of the whole string.

As an example for :mod:`datetime` we use format specifiers from
:ref:`strftime-strptime-behavior` which would give us::
As an example for :mod:`datetime` we could use format specifiers described in
:ref:`strftime-strptime-behavior`::

>>> f"{now:%B %d, %Y}"
'July 28, 2020'

Most built-in types will comply with the :ref:`formatspec`.::
Most built-in types will comply with the :ref:`formatspec`::

>>> num = 12.3456
>>> f"{num:20}"
Expand All @@ -2530,7 +2530,7 @@ Most built-in types will comply with the :ref:`formatspec`.::

When a format specifier is given together with the equal sign ``'='`` the
default conversion for the expressions' is :func:`str`. Conversion can be used
to show the :func:`repr` form.::
to show the :func:`repr` form::

>>> import decimal
>>> value = decimal.Decimal("12.34567")
Expand All @@ -2554,8 +2554,15 @@ A consequence of sharing the same syntax as regular string literals is
that characters in the replacement fields must not conflict with the
quoting used in the outer formatted string literal::

f"abc {a["x"]} def" # error: outer string literal ended prematurely
f"abc {a['x']} def" # workaround: use different quoting
>>> a = {"Terry":"Jones", "John":"Cleese", "x":"Gilliam", "Eric":"Idle"}
>>> f"abc {a["x"]} def"
File "<stdin>", line 1
f"abc {a["x"]} def"
^
SyntaxError: f-string: unmatched '['

>>> f"abc {a['x']} def" # workaround: use different quoting
'abc Gilliam def'

Backslashes are not allowed in format expressions and will raise
an error::
Expand Down
0