This method attempts to replace the format output definition symbol (%) of the early C-like language style by using curly brackets ({}) and colons (:) in the string. The format() method can accept an unlimited number of parameters, and its display position can also be different from the order of occurrence.
The internal parameters of the method are the variables we want to format the output. If no location is specified, fill in the blank in {} in sequence by default, that is, the 0-th parameter in format() is filled in the 0-th {}, the 1-th parameter in format() is filled in the 1-th {}, and so on.
1, Format with% symbol
The format rules are as follows
'% [-] [+] [0][m][.n] format character'% x '
[-]: specify left output alignment
[+]: add a positive sign to the output
[0]: fill in zero in the specified space
[m] : Specifies the minimum width
[. n]: specify the precision and keep several decimal places
x: Objects requiring formatting
Formatter | significance | |
'd' |
| |
'i' | Returns the decimal representation of the object to format, if possible | |
'o' |
| |
'u' | Same as formatter'd ' | |
'x' |
| |
'X' | Returns the hexadecimal representation of the object to be formatted, if possible [use '0X' if required] | |
'e' | Returns the representation of the floating-point scientific count of the object to be formatted, if you can [use 'e'] | |
'E' | Returns the representation of the floating-point scientific count of the object to be formatted, if you can [use 'E'] | |
'f' |
| |
'F' | Returns the floating-point representation of the object to format, if possible | |
'g' | Use lowercase scientific count format. If the index is less than - 4 or not less than precision, otherwise return decimal format | |
'G' | Use uppercase scientific count format. If the exponent is less than - 4 or not less than precision, otherwise return decimal format | |
'c' |
| |
'r' |
| |
's' | Returns the name of the object to format__ str__ Return value of () method | |
'%' | That is, one for every two% |
The simplest syntax structure is as follows
'% format character'% x '
'%f'%4 Out[31]: '4.000000'
The specific usage of the format is as follows
1.[+]
'%+d'%5 Out[32]: '+5'
2.[.n]
'%.2f'%1234 Out[33]: '1234.00'
3.x
'%d,%c'%(65,65)#Character formatting multiple objects using tuples
In fact,% --- format characters are the formats that affect our input variables,
Not in this format range is equivalent to string addition
#Splice at the back '%.2f element'%123 Out[36]: '123.00 element' '%+d individual sb'%5 Out[37]: '+5 individual sb'
Splice at front 'This is%d'%5 Out[38]: 'This is 5'
2, Use the format() method to format the string
1. Fill the string by position
>>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{0}{1}{0}'.format('a', 'b') 'aba'
2. Filling and alignment
: there is a filling character after the sign. It can only be one character. If it is not specified, it is filled with spaces by default
^/< / > respectively, align left and right, with width at the back
>>> "I am{0},I like numbers{1}".format("spy",'65') 'I am spy,I like the number 65' >>> "I am{0},I like numbers{1:*^8}".format("spy",'56') 'I am spy,I like numbers***56***' >>> "I am{0},I like numbers{1: ^8}".format("spy",'76') 'I am spy,I like the number 76 ' >>> "I am{0},I like numbers{1: <8}".format("spy",'86') 'I am spy,I like the Number 86'
3. Fill the string with the key
>>> print 'hello {name1} i am {name2}'.format(name1='Kevin',name2='Tom') hello Kevin i am Tom
4. Fill the string with subscripts
>>> names=['Kevin','Tom'] >>> print 'hello {names[0]} i am {names[1]}'.format(names=names) >>> print 'hello {0[0]} i am {0[1]}'.format(names) hello Kevin i am Tom
5. Use "," as thousands separator
'{:,}'.format(1234567890) '1,234,567,890'
6. Percentage display
>>> points = 19 >>> total = 22 >>> 'Correct answers: {:.2%}'.format(points/total) 'Correct answers: 86.36%'
7. Decimal places retention
>>>'{:.2f}'.format(3.1415926) 3.14 >>> '{:.0f}'.format(3.1415926) 3
3, Expressions used in Python f-string s
We can put the expression between {} square brackets, as follows:
bags = 3 apples_in_bag = 12 # Evaluate an expression in f-string print(f'There are total of {bags * apples_in_bag} apples')
There are total of 36 apples
Formatting floats in Python f-string
val = 12.3 print(f'{val:.2f}') print(f'{val:.5f}')
12.30 12.30000
Character width in Python f-string
for x in range(1, 11): print(f'{x:02} {x*x:3} {x*x*x:4}')
01 1 1 02 4 8 03 9 27 04 16 64 05 25 125 06 36 216 07 49 343 08 64 512 09 81 729 10 100 1000
Python f-string alignment string
s1 = 'a' s2 = 'ab' s3 = 'abc' s4 = 'abcd' print(f'{s1:>10}') print(f'{s2:>10}') print(f'{s3:>10}') print(f'{s4:>10}')
a ab abc abcd