Skip to content

Commit 9641c06

Browse files
committed
Move above printf-style
1 parent d01da75 commit 9641c06

File tree

1 file changed

+130
-131
lines changed

1 file changed

+130
-131
lines changed

Diff for: Doc/library/stdtypes.rst

+130-131
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,136 @@ expression support in the :mod:`re` module).
24562456
'-0042'
24572457

24582458

2459+
.. index::
2460+
single: ! formatted string literal
2461+
single: formatted string literals
2462+
single: ! f-string
2463+
single: f-strings
2464+
single: fstring
2465+
single: interpolated string literal
2466+
single: string; formatted literal
2467+
single: string; interpolated literal
2468+
single: {} (curly brackets); in formatted string literal
2469+
single: ! (exclamation mark); in formatted string literal
2470+
single: : (colon); in formatted string literal
2471+
single: = (equals); for help in debugging using string literals
2472+
2473+
Formatted String Literals (f-strings)
2474+
-------------------------------------
2475+
2476+
.. versionadded:: 3.6
2477+
2478+
An :dfn:`f-string` (formally a :dfn:`formatted string literal`) is
2479+
a string literal that is prefixed with ``f`` or ``F``.
2480+
This type of string literal allows embedding arbitrary Python expressions
2481+
within *replacement fields*, which are delimited by curly brackets (``{}``).
2482+
These expressions are evaluated at runtime, similarly to :meth:`str.format`,
2483+
and are converted into regular :class:`str` objects.
2484+
For example:
2485+
2486+
.. code-block:: pycon
2487+
2488+
>>> who = 'nobody'
2489+
>>> nationality = 'Spanish'
2490+
>>> f'{who.title()} expects the {nationality} Inquisition!'
2491+
'Nobody expects the Spanish Inquisition!'
2492+
2493+
It is also possible to use a multi line f-string:
2494+
2495+
.. code-block:: pycon
2496+
2497+
>>> f'''This is a string
2498+
... on two lines'''
2499+
'This is a string\non two lines'
2500+
2501+
A single opening curly bracket, ``'{'``, marks a *replacement field* that
2502+
can contain any Python expression:
2503+
2504+
.. code-block:: pycon
2505+
2506+
>>> nationality = 'Spanish'
2507+
>>> f'The {name} Inquisition!'
2508+
'The Spanish Inquisition!'
2509+
2510+
To include a literal ``{`` or ``}``, use a double bracket:
2511+
2512+
.. code-block:: pycon
2513+
2514+
>>> x = 42
2515+
>>> f'{{x}} is {x}'
2516+
'{x} is 42'
2517+
2518+
Functions can also be used, and :ref:`format specifier <formatstrings>`:
2519+
2520+
.. code-block:: pycon
2521+
2522+
>>> from math import sqrt
2523+
>>> f'√2 \N{ALMOST EQUAL TO} {sqrt(2):.5f}'
2524+
'√2 ≈ 1.41421'
2525+
2526+
Any non-string expression is converted using :func:`str`, by default:
2527+
2528+
.. code-block:: pycon
2529+
2530+
>>> from fractions import Fraction
2531+
>>> f'{Fraction(1, 3)}'
2532+
'1/3'
2533+
2534+
To use an explicit conversion, use the ``!`` (exclamation mark) operator,
2535+
followed by any of the valid formats, which are:
2536+
2537+
========== ==============
2538+
Conversion Meaning
2539+
========== ==============
2540+
``!a`` :func:`ascii`
2541+
``!r`` :func:`repr`
2542+
``!s`` :func:`str`
2543+
========== ==============
2544+
2545+
For example:
2546+
2547+
.. code-block:: pycon
2548+
2549+
>>> from fractions import Fraction
2550+
>>> f'{Fraction(1, 3)!s}'
2551+
'1/3'
2552+
>>> f'{Fraction(1, 3)!r}'
2553+
'Fraction(1, 3)'
2554+
>>> question = '¿Dónde está el Presidente?'
2555+
>>> print(f'{question!a}')
2556+
'\xbfD\xf3nde est\xe1 el Presidente?'
2557+
2558+
While debugging it may be helpful to see both the expression and its value,
2559+
using the equals sign (``=``) after the expression.
2560+
This preserves spaces within the brackets, and can be used with a converter.
2561+
By default, the debugging operator uses the :func:`repr` (``!r``) conversion.
2562+
For example:
2563+
2564+
.. code-block:: pycon
2565+
2566+
>>> from fractions import Fraction
2567+
>>> calculation = Fraction(1, 3)
2568+
>>> f'{calculation=}'
2569+
'calculation=Fraction(1, 3)'
2570+
>>> f'{calculation = }'
2571+
'calculation = Fraction(1, 3)'
2572+
>>> f'{calculation = !s}'
2573+
'calculation = 1/3'
2574+
2575+
Once the output has been evaluated, it can be formatted using a
2576+
:ref:`format specifier <formatstrings>` following a colon (``':'``).
2577+
After the expression has been evaluated, and possibly converted to a string,
2578+
the :meth:`__format__` method of the result is called with the format specifier,
2579+
or the empty string if no format specifier is given.
2580+
The formatted result is then used as the final value for the replacement field.
2581+
For example:
2582+
2583+
>>> from fractions import Fraction
2584+
>>> f'{Fraction(1, 7):.6f}'
2585+
'0.142857'
2586+
>>> f'{Fraction(1, 7):_^+10}'
2587+
'___+1/7___'
2588+
24592589

24602590
.. _old-string-formatting:
24612591

@@ -2657,137 +2787,6 @@ that ``'\0'`` is the end of the string.
26572787
longer replaced by ``%g`` conversions.
26582788

26592789

2660-
.. index::
2661-
single: ! formatted string literal
2662-
single: formatted string literals
2663-
single: ! f-string
2664-
single: f-strings
2665-
single: fstring
2666-
single: interpolated string literal
2667-
single: string; formatted literal
2668-
single: string; interpolated literal
2669-
single: {} (curly brackets); in formatted string literal
2670-
single: ! (exclamation mark); in formatted string literal
2671-
single: : (colon); in formatted string literal
2672-
single: = (equals); in debug string literal
2673-
2674-
Formatted String Literals (f-strings)
2675-
-------------------------------------
2676-
2677-
.. versionadded:: 3.6
2678-
2679-
An :dfn:`f-string` (formally a :dfn:`formatted string literal`) is
2680-
a string literal that is prefixed with ``f`` or ``F``.
2681-
This type of string literal allows embedding arbitrary Python expressions
2682-
within *replacement fields*, which are delimited by curly brackets (``{}``).
2683-
These expressions are evaluated at runtime, similarly to :meth:`str.format`,
2684-
and are converted into regular :class:`str` objects.
2685-
For example:
2686-
2687-
.. code-block:: pycon
2688-
2689-
>>> who = 'nobody'
2690-
>>> nationality = 'Spanish'
2691-
>>> f'{who.title()} expects the {nationality} Inquisition!'
2692-
'Nobody expects the Spanish Inquisition!'
2693-
2694-
It is also possible to use a multi line f-string:
2695-
2696-
.. code-block:: pycon
2697-
2698-
>>> f'''This is a string
2699-
... on two lines'''
2700-
'This is a string\non two lines'
2701-
2702-
A single opening curly bracket, ``'{'``, marks a *replacement field* that
2703-
can contain any Python expression:
2704-
2705-
.. code-block:: pycon
2706-
2707-
>>> nationality = 'Spanish'
2708-
>>> f'The {name} Inquisition!'
2709-
'The Spanish Inquisition!'
2710-
2711-
To include a literal ``{`` or ``}``, use a double bracket:
2712-
2713-
.. code-block:: pycon
2714-
2715-
>>> x = 42
2716-
>>> f'{{x}} is {x}'
2717-
'{x} is 42'
2718-
2719-
Functions can also be used, and :ref:`format specifier <formatstrings>`:
2720-
2721-
.. code-block:: pycon
2722-
2723-
>>> from math import sqrt
2724-
>>> f'√2 \N{ALMOST EQUAL TO} {sqrt(2):.5f}'
2725-
'√2 ≈ 1.41421'
2726-
2727-
Any non-string expression is converted using :func:`str`, by default:
2728-
2729-
.. code-block:: pycon
2730-
2731-
>>> from fractions import Fraction
2732-
>>> f'{Fraction(1, 3)}'
2733-
'1/3'
2734-
2735-
To use an explicit conversion, use the ``!`` (exclamation mark) operator,
2736-
followed by any of the valid formats, which are:
2737-
2738-
========== ==============
2739-
Conversion Meaning
2740-
========== ==============
2741-
``!a`` :func:`ascii`
2742-
``!r`` :func:`repr`
2743-
``!s`` :func:`str`
2744-
========== ==============
2745-
2746-
For example:
2747-
2748-
.. code-block:: pycon
2749-
2750-
>>> from fractions import Fraction
2751-
>>> f'{Fraction(1, 3)!s}'
2752-
'1/3'
2753-
>>> f'{Fraction(1, 3)!r}'
2754-
'Fraction(1, 3)'
2755-
>>> question = '¿Dónde está el Presidente?'
2756-
>>> print(f'{question!a}')
2757-
'\xbfD\xf3nde est\xe1 el Presidente?'
2758-
2759-
While debugging it may be helpful to see both the expression and its value,
2760-
using the equals sign (``=``) after the expression.
2761-
This preserves spaces within the brackets, and can be used with a converter.
2762-
By default, the debugging operator uses the :func:`repr` (``!r``) conversion.
2763-
For example:
2764-
2765-
.. code-block:: pycon
2766-
2767-
>>> from fractions import Fraction
2768-
>>> calculation = Fraction(1, 3)
2769-
>>> f'{calculation=}'
2770-
'calculation=Fraction(1, 3)'
2771-
>>> f'{calculation = }'
2772-
'calculation = Fraction(1, 3)'
2773-
>>> f'{calculation = !s}'
2774-
'calculation = 1/3'
2775-
2776-
Once the output has been evaluated, it can be formatted using a
2777-
:ref:`format specifier <formatstrings>` following a colon (``':'``).
2778-
After the expression has been evaluated, and possibly converted to a string,
2779-
the :meth:`__format__` method of the result is called with the format specifier,
2780-
or the empty string if no format specifier is given.
2781-
The formatted result is then used as the final value for the replacement field.
2782-
For example:
2783-
2784-
>>> from fractions import Fraction
2785-
>>> f'{Fraction(1, 7):.6f}'
2786-
'0.142857'
2787-
>>> f'{Fraction(1, 7):_^+10}'
2788-
'___+1/7___'
2789-
2790-
27912790
.. index::
27922791
single: buffer protocol; binary sequence types
27932792

0 commit comments

Comments
 (0)