个人理解:可以按照顺序进行格式化,其中需要进行替代的参数只要能够获取到就行。例如:
>>> c = 3-5j >>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c) 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' >>> class Point(object): ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ... >>> str(Point(4, 2)) 'Point(4, 2)'在某些情况下,需要把格式化的参数先替换成字符串,然后再进行格式化,这样,conversion参数就有用武之地了。
Two conversion flags are currently supported: '!s' which calls str() on the value, and '!r' which calls repr().
现仅支持两类conversion标识:"!s"和"!r", "!s"会调用替换值得str(),而'!r'则会调用repr()
"Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first在有对齐的情况下,对空格进行替换的字符,可以是任何字符。
sign option只对数字类型的数字有效,有以下几种:
'+': indicates that a sign should be used for both positive as well as negative numbers.无论正负数,都会加上正负号。 '-': indicates that a sign should be used only for negative numbers (this is the default behavior).只有在数字为负数时会添加上负号 space: indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.在正数前面有个空格,在负数前面是个负号>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000'仅对整型数据有效,并且只能输出二进制、八进制或十六进制的转换数据,获得的输出数据前缀依次是'0b', '0o', 或'0x'。
>>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'此可选参数表示格式化需要千位分隔符','。在本地化的分隔符中,用'n'整型表示类型来代替。
>>> '{:,}'.format(1234567890) '1,234,567,890'width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
width选项,是一个十进制整型数据,用来定义字段的最短宽度。如果没有指定,那么字段的宽度将由字段本身的内容决定。
precision是一个十进制数据
当一个浮点型数据需要被格式化'f'、'F'时,小数点后面需要保留的位数。当一个浮点型数据需要被格式化'g'、'G'时,小数点前后总共的位数。当一个非数字的字段需要被格式化时,表示最大保留的字段长度,即字段中多少个字符会被使用不能对整型数据使用
type参数需要分情况使用
General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.一般的格式化。如果给定的精度p>=1,这将对数字p进行四舍五入,然后根据大小将结果格式化为固定精度的数据或是科学计数的数据。精度的规则为:将结果格式化为科学计数e和精度为p-1的表达式exp。如果-4<=exp<p,那么数据将会转化为'f'类型,并且精度为p-1。如果-4>exp,那么数据将会被格式化为'e'类型,精度为p-1.在这两种情况中,末尾的0将会移除,如果没有小数,小数点也会移除
>> '{:.10g}'.format(0.00097867008822282723426) '0.0009786700882' >> '{:.10g}'.format(0.000097867008822282723426) '9.786700882e-05' >> '{:.10g}'.format(0.0000900) '9e-05' >> '{:.10g}'.format(0.000900) '0.0009' >> '{:.10g}'.format(0.0000900) '9e-05' >> '{:.10g}'.format(0.0000000097867008822282723426) '9.786700882e-09'正负无穷,正0负0和 nans将会依次被格式化为inf,-inf,0, -0,不管精度为何值。精度0将被看做和精度1相同。默认的精度为6
https://docs.python.org/2/library/string.html