@@ -2320,6 +2320,146 @@ expression support in the :mod:`re` module).
23202320 '-0042'
23212321
23222322
2323+ .. index ::
2324+ single: ! formatted string literal
2325+ single: formatted string literals
2326+ single: ! f-string
2327+ single: f-strings
2328+ single: fstring
2329+ single: interpolated string literal
2330+ single: string; formatted literal
2331+ single: string; interpolated literal
2332+ single: {} (curly brackets); in formatted string literal
2333+ single: ! (exclamation mark); in formatted string literal
2334+ single: : (colon); in formatted string literal
2335+ single: = (equals); for help in debugging using string literals
2336+
2337+ Formatted String Literals (f-strings)
2338+ -------------------------------------
2339+
2340+ .. versionadded :: 3.6
2341+ .. versionchanged :: 3.7
2342+ The :keyword: `await ` and :keyword: `async for ` can be used in expressions
2343+ within f-strings.
2344+ .. versionchanged :: 3.8
2345+ Added the debugging operator (``= ``)
2346+ .. versionchanged :: 3.12
2347+ Many restrictions on expressions within f-strings have been removed.
2348+ Notably, nested strings, comments, and backslashes are now permitted.
2349+
2350+ An :dfn: `f-string ` (formally a :dfn: `formatted string literal `) is
2351+ a string literal that is prefixed with ``f `` or ``F ``.
2352+ This type of string literal allows embedding arbitrary Python expressions
2353+ within *replacement fields *, which are delimited by curly brackets (``{} ``).
2354+ These expressions are evaluated at runtime, similarly to :meth: `str.format `,
2355+ and are converted into regular :class: `str ` objects.
2356+ For example:
2357+
2358+ .. doctest ::
2359+
2360+ >>> who = ' nobody'
2361+ >>> nationality = ' Spanish'
2362+ >>> f ' { who.title()} expects the { nationality} Inquisition! '
2363+ 'Nobody expects the Spanish Inquisition!'
2364+
2365+ It is also possible to use a multi line f-string:
2366+
2367+ .. doctest ::
2368+
2369+ >>> f ''' This is a string
2370+ ... on two lines'''
2371+ 'This is a string\non two lines'
2372+
2373+ A single opening curly bracket, ``'{' ``, marks a *replacement field * that
2374+ can contain any Python expression:
2375+
2376+ .. doctest ::
2377+
2378+ >>> nationality = ' Spanish'
2379+ >>> f ' The { nationality} Inquisition! '
2380+ 'The Spanish Inquisition!'
2381+
2382+ To include a literal ``{ `` or ``} ``, use a double bracket:
2383+
2384+ .. doctest ::
2385+
2386+ >>> x = 42
2387+ >>> f ' {{ x }} is { x} '
2388+ '{x} is 42'
2389+
2390+ Functions can also be used, and :ref: `format specifiers <formatstrings >`:
2391+
2392+ .. doctest ::
2393+
2394+ >>> from math import sqrt
2395+ >>> f ' √2 \N{ALMOST EQUAL TO} { sqrt(2 ):.5f } '
2396+ '√2 ≈ 1.41421'
2397+
2398+ Any non-string expression is converted using :func: `str `, by default:
2399+
2400+ .. doctest ::
2401+
2402+ >>> from fractions import Fraction
2403+ >>> f ' { Fraction(1 , 3 )} '
2404+ '1/3'
2405+
2406+ To use an explicit conversion, use the ``! `` (exclamation mark) operator,
2407+ followed by any of the valid formats, which are:
2408+
2409+ ========== ==============
2410+ Conversion Meaning
2411+ ========== ==============
2412+ ``!a `` :func: `ascii `
2413+ ``!r `` :func: `repr `
2414+ ``!s `` :func: `str `
2415+ ========== ==============
2416+
2417+ For example:
2418+
2419+ .. doctest ::
2420+
2421+ >>> from fractions import Fraction
2422+ >>> f ' { Fraction(1 , 3 )!s } '
2423+ '1/3'
2424+ >>> f ' { Fraction(1 , 3 )!r } '
2425+ 'Fraction(1, 3)'
2426+ >>> question = ' ¿Dónde está el Presidente?'
2427+ >>> print (f ' { question!a } ' )
2428+ '\xbfD\xf3nde est\xe1 el Presidente?'
2429+
2430+ While debugging it may be helpful to see both the expression and its value,
2431+ by using the equals sign (``= ``) after the expression.
2432+ This preserves spaces within the brackets, and can be used with a converter.
2433+ By default, the debugging operator uses the :func: `repr ` (``!r ``) conversion.
2434+ For example:
2435+
2436+ .. doctest ::
2437+
2438+ >>> from fractions import Fraction
2439+ >>> calculation = Fraction(1 , 3 )
2440+ >>> f ' { calculation= } '
2441+ 'calculation=Fraction(1, 3)'
2442+ >>> f ' { calculation = } '
2443+ 'calculation = Fraction(1, 3)'
2444+ >>> f ' { calculation = !s } '
2445+ 'calculation = 1/3'
2446+
2447+ Once the output has been evaluated, it can be formatted using a
2448+ :ref: `format specifier <formatstrings >` following a colon (``':' ``).
2449+ After the expression has been evaluated, and possibly converted to a string,
2450+ the :meth: `!__format__ ` method of the result is called with the format specifier,
2451+ or the empty string if no format specifier is given.
2452+ The formatted result is then used as the final value for the replacement field.
2453+ For example:
2454+
2455+ .. doctest ::
2456+
2457+ >>> from fractions import Fraction
2458+ >>> f ' { Fraction(1 , 7 ):.6f } '
2459+ '0.142857'
2460+ >>> f ' { Fraction(1 , 7 ):_^+10 } '
2461+ '___+1/7___'
2462+
23232463
23242464.. _old-string-formatting :
23252465
0 commit comments