format
---恢复内容开始---
tp1='i am {},age{},{}'.format("seven",18,'alex')#不一一对应会报错 print(tp1)
输出
i am seven,age18,alex
tp2='i am {},age{},{}'.format("seven",18) print(tp2)
输出报错
IndexError: tuple index out of range
tp2='i am {2},age{1},{0}'.format("seven",18,'alex') print(tp2)
输出
i am alex,age18,seven
tp2='i am {1},age{1}'.format("seven",18,'alex') print(tp2)
输出
i am 18,age18
tp2='i am {name},age{age},really{name}'.format(name="seven",age=18) print(tp2)
输出
i am seven,age18,reallyseven
tp2='i am {name},age{age},really{name}'.format(**{'name':"seven",'age':18}) print(tp2)
输出
i am seven,age18,reallyseven
tp1='i am {0[0]},age{0[1]},really{0[2]}'.format([1,2,3],[11,22,33]) print(tp1)
输出
i am 1,age2,really3
tp1='i am {:s},age{:d},really{:f}'.format("seven",18,88.7) print(tp1)
输出
i am seven,age18,really88.700000
tp1='i am {name:s},age{age:d}'.format(name="seven",age=18) print(tp1)
输出
i am seven,age18
tp1 = 'i am {name:s},age{age:d}'.format(**{'name': "seven", 'age': 18}) print(tp1)
输出
i am seven,age18
:b二进制 :o八进制 :d整 :x十六进制 :%显示百分比(默认小数点后六位)
多了可以少了不行
tp1 = 'nambers:{:b},{:o},{:d},{:x},{:x},{:%}'.format(15,15,15,15,15,15.87623,2) print(tp1)
输出
nambers:1111,17,15,f,f,1587.623000%
---恢复内容结束---