转载自https://www.cnblogs.com/qinchao0317/p/10699717.html
左中右对齐及位数补全
(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐
(2)取位数“{:4s}”、"{:.2f}"等
>>> print('{} and {}'.format('hello','world')) # 默认左对齐
hello and world
>>> print('{:10s} and {:>10s}'.format('hello','world')) # 取10位左对齐,取10位右对齐
hello and world
>>> print('{:^10s} and {:^10s}'.format('hello','world')) # 取10位中间对齐
hello and world
>>> print('{} is {:.2f}'.format(1.123,1.123)) # 取2位小数
1.123 is 1.12
>>> print('{0} is {0:>10.2f}'.format(1.123)) # 取2位小数,右对齐,取10位
1.123 is 1.12
>>> '{:<30}'.format('left aligned') # 左对齐
'left aligned '
>>> '{:>30}'.format('right aligned') # 右对齐
' right aligned'
>>> '{:^30}'.format('centered') # 中间对齐
' centered '
>>> '{:*^30}'.format('centered') # 使用“*”填充
'***********centered***********'
>>>'{:0=30}'.format(11) # 还有“=”只能应用于数字,这种方法可用“>”代替
'000000000000000000000000000011'
左中右对齐及位数补齐