1. format 格式化
格式:"{<参数序号>:<填充><对齐><宽度><,><.精度><类型>}".format(<参数>)
简单实例:
# format 的简单使用
print('{}'.format('hello')) # hello
# 默认按照参数顺序填入
print('{}-{}-{}'.format('hello', 'a', 'b')) # hello-a-b
# 也可以使用参数序号指定填入的位置
print('{1}-{2}-{0}'.format('hello', 'a', 'b')) # a-b-hello
# 用 . 指定数字显示的位数
print('{0:.1}-{0:.2}-{0:.3}'.format(3.1415926)) # 3e+00-3.1-3.14
# 在后面加入 f 才是指定浮点数小数的位数
print('{0:.1f}-{0:.2f}-{0:.3f}'.format(3.1415926)) # 3.1-3.14-3.142
# 将数字分别以 二进制、八进制、十进制和十六进制 的形式输出
print('{0:b}-{0:o}-{0:d}-{0:x}'.format(17)) # 10001-21-17-11
# 将数字以 ASCII码符 的形式输出
print('{:c}-{:c}-{:c}-{:c}-{:c}-{:c}'.format(65, 66, 67, 97, 98, 99)) # A-B-C-a-b-c
# 居中显示,指定宽度为 10,默认以空格填充
print('-{:^10}-'.format('hello'