Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AA-Turner
Copy link
Member

@AA-Turner AA-Turner commented Apr 18, 2025

@python-cla-bot

This comment was marked as resolved.

@bedevere-app bedevere-app bot added awaiting core review docs Documentation in the Doc dir skip news labels Apr 18, 2025
@AA-Turner AA-Turner marked this pull request as draft April 18, 2025 15:01
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Apr 18, 2025
@AA-Turner AA-Turner requested a review from ezio-melotti April 18, 2025 15:41
@AA-Turner AA-Turner changed the title Docs: Add overview of formatted string literals (f-strings) to str gh-85583: Add overview of formatted string literals (f-strings) to str Apr 18, 2025
Copy link
Contributor

@StanFromIreland StanFromIreland left a 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!'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> 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
Copy link
Contributor

@StanFromIreland StanFromIreland Apr 18, 2025

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,

Comment on lines +2492 to +2496
For example:

.. code-block:: pycon

>>> who = 'nobody'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For example:
.. code-block:: pycon
>>> who = 'nobody'
For example::
>>> who = 'nobody'

Comment on lines +2501 to +2505
It is also possible to use a multi line f-string:

.. code-block:: pycon

>>> f'''This is a string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +2526 to +2530
Functions can also be used, and :ref:`format specifier <formatstrings>`:

.. code-block:: pycon

>>> from math import sqrt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +2534 to +2538
Any non-string expression is converted using :func:`str`, by default:

.. code-block:: pycon

>>> from fractions import Fraction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +2553 to +2557
For example:

.. code-block:: pycon

>>> from fractions import Fraction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For example:
.. code-block:: pycon
>>> from fractions import Fraction
For example::
>>> from fractions import Fraction

Comment on lines +2570 to +2574
For example:

.. code-block:: pycon

>>> from fractions import Fraction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For example:
For example::

Is it not required?

@skirpichev skirpichev self-requested a review April 19, 2025 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

2 participants