All the rules that are included are described first
Curly brace Declaration {}: parameter reference declaration before rendering, curly brackets can be used to denote the ordinal number of the reference parameter, or the variable name is directly referenced. The variable name introduced from the format parameter colon: The number of character bits declares the declaration of a blank auto-fill character declaration of a variable-type declaration: string s, number D, float f-align direction symbol < ^ > property accessor in parentheses? Use an exclamation point! followed by A, R, S, the declaration is the mode of use, acsii mode, reference __repr__ or __str__ Add Class Magic function __format__ (self, format), can be based on the format of the string before format to customize the different display, such as: ' {: xxxx the XXXX will be passed into the __FORMAT__ function as a parameter at this time.
Next look at the specific use
>>> ' {: >18,.2f} '. Format (70305084.0) #: Colon + blank padding + right-aligned + fixed-width 18 + floating-point precision. F ' 70,305,084.00 ' in floating-point number declaration
>>> data = [4, 8, +, 23, 42]>>> ' {d[4]} {d[5]} '. Format (d=data) ' 42 '
>>> class Plant (object): ... Type = ' Tree ' ... kinds = [{' name ': ' Oak '}, {' name ': ' Maple '}] ... >>> ' {P.type}: {P.kinds[0][name]} '. Format (p=plant ()) ' Tree:oak ‘
>>> ' {:. 5} '. Format (' xylophone ') ' xylop '
>>> ' {: 4d} '. Format ( 42 ')
>>> ' {:. {}} '. Format (' xylophone ', 7) ' Xylopho '
>>> ' {: 06.2f} '. Format (3.141592653589793) #填补符 + fixed width + floating point precision + variable type ' 003.14 '
>>> from datetime import datetime>>> ' {:%y-%m-%d%h:%m} '. Format (DateTime (2001, 2, 3, 4, 5)) ' 2001-02-03 04:05 '
[Python] Formatting method format