-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
gh-85583: Add overview of formatted string literals (f-strings) to str
#132689
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
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: Ezio Melotti <[email protected]> Originally-authored-by: [email protected]
9641c06
to
8d4bf9c
Compare
str
str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also more links to other pages? e.g. lexical analysis
(Should the title not be "...to stdtypes
"?)
.. code-block:: pycon | ||
|
||
>>> nationality = 'Spanish' | ||
>>> f'The {name} Inquisition!' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> f'The {name} Inquisition!' | |
>>> f'The {nationality} Inquisition!' |
Error
A single opening curly bracket, ``'{'``, marks a *replacement field* that | ||
can contain any Python expression: | ||
|
||
.. code-block:: pycon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these still needed, sphinx is smart enough to detect the '>>>' in other files, why should it be different here?
I see later you don't use it?
Once the output has been evaluated, it can be formatted using a | ||
:ref:`format specifier <formatstrings>` following a colon (``':'``). | ||
After the expression has been evaluated, and possibly converted to a string, | ||
the :meth:`__format__` method of the result is called with the format specifier, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the :meth:`__format__` method of the result is called with the format specifier, | |
the :meth:`~object.__format__` method of the result is called with the format specifier, |
For example: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> who = 'nobody' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example: | |
.. code-block:: pycon | |
>>> who = 'nobody' | |
For example:: | |
>>> who = 'nobody' |
It is also possible to use a multi line f-string: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> f'''This is a string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is also possible to use a multi line f-string: | |
.. code-block:: pycon | |
>>> f'''This is a string | |
It is also possible to use a multi line f-string:: | |
>>> f'''This is a string |
Functions can also be used, and :ref:`format specifier <formatstrings>`: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> from math import sqrt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functions can also be used, and :ref:`format specifier <formatstrings>`: | |
.. code-block:: pycon | |
>>> from math import sqrt | |
Functions can also be used, and :ref:`format specifier <formatstrings>`:: | |
>>> from math import sqrt |
Any non-string expression is converted using :func:`str`, by default: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> from fractions import Fraction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any non-string expression is converted using :func:`str`, by default: | |
.. code-block:: pycon | |
>>> from fractions import Fraction | |
Any non-string expression is converted using :func:`str`, by default:: | |
>>> from fractions import Fraction |
For example: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> from fractions import Fraction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example: | |
.. code-block:: pycon | |
>>> from fractions import Fraction | |
For example:: | |
>>> from fractions import Fraction |
For example: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> from fractions import Fraction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example: | |
.. code-block:: pycon | |
>>> from fractions import Fraction | |
For example:: | |
>>> from fractions import Fraction |
the :meth:`__format__` method of the result is called with the format specifier, | ||
or the empty string if no format specifier is given. | ||
The formatted result is then used as the final value for the replacement field. | ||
For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example: | |
For example:: |
Is it not required?
Replaces #21552.
📚 Documentation preview 📚: https://cpython-previews--132689.org.readthedocs.build/en/132689/library/stdtypes.html#formatted-string-literals-f-strings