在 Python 中, format 方法就是用参变量的值代替格式符。更多细节如下:
更多关于格式在 Python Enhancement Proposal No.3101 中有解释。
>>> '{0:.3}'.format(1/3) # decimal (.) precision of 3 for float '0.333'
>>> '{0:_^11}'.format('hello') # fill with underscores (_) with the
text
centered (^) to 11 width
'___hello___'
>>> '{name} wrote {book}'.format(name='Swaroop', book='A Byte of
Python')
# keyword-based
'Swaroop wrote A Byte of Python'