## Python Tutorial, Release 3.9.5 

>>> s ='Hello, world. ' >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" >>> str(1/7) '0.14285714285714285' >>> x =10*3.25 >>> y =200 *200 >>> s ='The value of x is'+repr(x)+', and y is'+repr(y)+'... ' >>> print(s) The value of x is 32.5, and y is 40000... >>> # The repr() of a string adds string quotes and backslashes: ... hello ='hello, world 
 ' >>> hellos =repr(hello) >>> print(hellos) 'hello, world
' >>> # The argument to repr() may be any Python object: ... repr((x, y, ( 'spam','eggs'))) "(32.5, 40000, ('spam', 'eggs'))" 

The$_{string}$ module contains a$_{Template}$class that offers yet another way to substitute values into strings, using placeholders like$_{$x}$ and replacing them with values from a dictionary, but offers much less control of the formatting. 

## 7.1.1 Formatted String Literals 

Formatted string literals (also called f-strings for short) let you include the value of Python expressions inside a string by prefixing the string with$_{f}$ or$_{F}$ and writing expressions as$_{{expression}}$ . 

An optional format specifier can follow the expression. This allows greater control over how the value is formatted. The following example rounds pi to three places after the decimal: 

>>>import math >>> print(f'The value of pi is approximately { math.pi : .3f } .') The value of pi is approximately 3.142. 

Passing an integer after the$_{':'}$ will cause that field to be a minimum number of characters wide. This is useful for making columns line up. 

>>> table={ 'Sjoerd':4127 ,'Jack':4098 ,'Dcab':7678 } >>>for name, phone in table.items(): ... print(f' { name : 10 } ==> { phone : 10d } ') ... Sjoerd ==> 4127 Jack ==> 4098 Dcab ==> 7678 

Other modifiers can be used to convert the value before it is formatted. '!a' applies$_{ascii()}$ ,$_{'!s'}$ applies str() , and$_{'!r'}$ applies$_{repr()}$ : 

>>> animals='eels' >>> print(f'My hovercraft is full of { animals } .') My hovercraft is full of eels. >>> print(f'My hovercraft is full of { animals !r} .') My hovercraft is full of 'eels'. 

For a reference on these format specifications, see the reference guide for the formatspec. 

