Use the Python format () function to copy the blog from blogger Chunlaipiupiupiu, if there is infringement, please contact delete
Format function for string formatting in Python
by keyword
1 print (' {name} ' today {action} '. Format (name = ' Chen ', action = ' shoot video ') #通过关键字2 grade = {' name ': ' Chen ', ' Fenshu ': '}3 ' Print (' {name '} Electrician tested { Fenshu} '. Format (**grade)) #通过关键字, use a dictionary when the keyword passes in the value, add * * in front of the dictionary
By location
1 print (' {1} today {0} '. Format (' Shoot video ', ' Chen ') #通过位置2 print (' {0} ' today {1} '. Format (' Chen ', ' shoot video ')
Fill and align ^<> are centered, left-aligned, right-aligned, followed by width
1 print (' {: ^14} '. Format (' Chen ')) 2 print (' {: >14} ') 3 print (' {: <14} '. Format (' Chen ') ') 4 print (' {:* <14} '. Format (' Chen ') 5 print (' {: &>14} ') #填充和对齐 ^<> are centered, left-aligned, right-aligned, followed by width
Accuracy and type F accuracy often used with F
1 print (' {:. 1f} '. Format (4.234324525254)) 2 print (' {:. 4f} '. Format (4.1))
Binary conversion, b o D x represents two or eight, 16 or 16 binary respectively
Print (' {: b} '. Format () print (' {: o} '. Format () print (' {:d} '. Format ()) print (' {: x} '. Format (250))
Thousand-bit separators, which are only for numbers
Print (' {:,} '. Format (100000000)) print (' {:,} '. Format (235445.234235))
Usage of the Python format () function