[[fill]align][sign][#][0][width][,][.precision][type]
# format值必须一一对应,否则会报错,多可以,少不行 # msg = "I am {},age{},love{}".format(‘kelsey‘, 18, ‘yy‘) # msg = "I am {2},age{1},love{0}".format(‘kelsey‘, 18, ‘yy‘) 元祖下标去取值 # msg = "I am {name},age {age},love {val}".format(name=‘kelsey‘, age=18, val=‘yy‘) # msg = "I am {name},age {age},love {val}".format(**{‘name‘: ‘kelsey‘, ‘age‘: 18, ‘val‘: ‘yy‘}) **字典dict # msg = "I am {0[2]},age {1[0]},love {1[1]}".format([1, 2, 3], [2, 3, 4]) # msg = "I am {:s},age {:d},love {:f}".format(‘kelsey‘, 18, 9.345673378993534444) # msg = "I am {:s},age {:d},love {:.2f}".format(*[‘kelsey‘, 18, 9.345673378993534444]) *传入列表 # msg = "I am {0},age {1},love {2}".format(*[‘kelsey‘, 18, 9.345673378993534444]) # :b二进制,:o八进制,:d整形,十进制,:x小写十六进制,:X大写十六进制,:%百分比 msg = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}, {}".format(15, 15, 15, 15, 15, 15.87623, 2) print(msg)
原文:https://www.cnblogs.com/kelseyJJ/p/12844247.html