@@ -2456,6 +2456,146 @@ 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+ .. versionchanged :: 3.7
2478+ The :keyword: `await ` and :keyword: `async for ` can be used in expressions
2479+ within f-strings.
2480+ .. versionchanged :: 3.8
2481+ Added the debugging operator (``= ``)
2482+ .. versionchanged :: 3.12
2483+ Many restrictions on expressions within f-strings have been removed.
2484+ Notably, nested strings, comments, and backslashes are now permitted.
2485+
2486+ An :dfn: `f-string ` (formally a :dfn: `formatted string literal `) is
2487+ a string literal that is prefixed with ``f `` or ``F ``.
2488+ This type of string literal allows embedding arbitrary Python expressions
2489+ within *replacement fields *, which are delimited by curly brackets (``{} ``).
2490+ These expressions are evaluated at runtime, similarly to :meth: `str.format `,
2491+ and are converted into regular :class: `str ` objects.
2492+ For example:
2493+
2494+ .. doctest ::
2495+
2496+ >>> who = ' nobody'
2497+ >>> nationality = ' Spanish'
2498+ >>> f ' { who.title()} expects the { nationality} Inquisition! '
2499+ 'Nobody expects the Spanish Inquisition!'
2500+
2501+ It is also possible to use a multi line f-string:
2502+
2503+ .. doctest ::
2504+
2505+ >>> f ''' This is a string
2506+ ... on two lines'''
2507+ 'This is a string\non two lines'
2508+
2509+ A single opening curly bracket, ``'{' ``, marks a *replacement field * that
2510+ can contain any Python expression:
2511+
2512+ .. doctest ::
2513+
2514+ >>> nationality = ' Spanish'
2515+ >>> f ' The { nationality} Inquisition! '
2516+ 'The Spanish Inquisition!'
2517+
2518+ To include a literal ``{ `` or ``} ``, use a double bracket:
2519+
2520+ .. doctest ::
2521+
2522+ >>> x = 42
2523+ >>> f ' {{ x }} is { x} '
2524+ '{x} is 42'
2525+
2526+ Functions can also be used, and :ref: `format specifiers <formatstrings >`:
2527+
2528+ .. doctest ::
2529+
2530+ >>> from math import sqrt
2531+ >>> f ' √2 \N{ALMOST EQUAL TO} { sqrt(2 ):.5f } '
2532+ '√2 ≈ 1.41421'
2533+
2534+ Any non-string expression is converted using :func: `str `, by default:
2535+
2536+ .. doctest ::
2537+
2538+ >>> from fractions import Fraction
2539+ >>> f ' { Fraction(1 , 3 )} '
2540+ '1/3'
2541+
2542+ To use an explicit conversion, use the ``! `` (exclamation mark) operator,
2543+ followed by any of the valid formats, which are:
2544+
2545+ ========== ==============
2546+ Conversion Meaning
2547+ ========== ==============
2548+ ``!a `` :func: `ascii `
2549+ ``!r `` :func: `repr `
2550+ ``!s `` :func: `str `
2551+ ========== ==============
2552+
2553+ For example:
2554+
2555+ .. doctest ::
2556+
2557+ >>> from fractions import Fraction
2558+ >>> f ' { Fraction(1 , 3 )!s } '
2559+ '1/3'
2560+ >>> f ' { Fraction(1 , 3 )!r } '
2561+ 'Fraction(1, 3)'
2562+ >>> question = ' ¿Dónde está el Presidente?'
2563+ >>> print (f ' { question!a } ' )
2564+ '\xbfD\xf3nde est\xe1 el Presidente?'
2565+
2566+ While debugging it may be helpful to see both the expression and its value,
2567+ by using the equals sign (``= ``) after the expression.
2568+ This preserves spaces within the brackets, and can be used with a converter.
2569+ By default, the debugging operator uses the :func: `repr ` (``!r ``) conversion.
2570+ For example:
2571+
2572+ .. doctest ::
2573+
2574+ >>> from fractions import Fraction
2575+ >>> calculation = Fraction(1 , 3 )
2576+ >>> f ' { calculation= } '
2577+ 'calculation=Fraction(1, 3)'
2578+ >>> f ' { calculation = } '
2579+ 'calculation = Fraction(1, 3)'
2580+ >>> f ' { calculation = !s } '
2581+ 'calculation = 1/3'
2582+
2583+ Once the output has been evaluated, it can be formatted using a
2584+ :ref: `format specifier <formatstrings >` following a colon (``':' ``).
2585+ After the expression has been evaluated, and possibly converted to a string,
2586+ the :meth: `!__format__ ` method of the result is called with the format specifier,
2587+ or the empty string if no format specifier is given.
2588+ The formatted result is then used as the final value for the replacement field.
2589+ For example:
2590+
2591+ .. doctest ::
2592+
2593+ >>> from fractions import Fraction
2594+ >>> f ' { Fraction(1 , 7 ):.6f } '
2595+ '0.142857'
2596+ >>> f ' { Fraction(1 , 7 ):_^+10 } '
2597+ '___+1/7___'
2598+
24592599
24602600.. _old-string-formatting :
24612601
0 commit comments