1.‘{1}..{0}‘.format(1,2,3):(1,2,3)看着元组,{1}{0},根据索引,取元组数据
s=‘I am {},my age is {},my love {}‘.format(‘wx‘,18,‘pyhton‘)#默认匹配填充print(s) s=‘I am {1},my age is {0}‘.format(‘wx‘,18,‘pyhton‘)#(‘wx‘,18,‘pyhton‘), 看着元组,{0},取对应索引的值print(s)
2.‘{1}..{0}‘.format(*[1,2,3]):*[1,2,3] ,*[],1个*,表示要以列表的方式传参
3.‘{name}..{age}‘.format(**{‘name‘:‘ff‘,‘age‘:18}):(**{‘name‘:‘ff‘,‘age‘:18}),**{},2个**,代表要以字典方式创参
4.等价于3,‘{name}..{age}‘.format(name=‘ff‘,age=18):(name=‘ff‘,age=18)等价于(**{‘name‘:‘ff‘,‘age‘:18}),注意:前者是变量名(key),后者是key名,‘key’名必须是字符串
5.复合型:
s=‘I am {1:b},my age is {2:o},my love {0:X},{3:%}‘.format(11,10,12,0.0128)#{1:b},1表示索引号是1,:b表示2进制形式,:X,大写16进制,{:%},百分比形式print(s)#返回 I am 1010,my age is 14,my love B,1.280000%
原文地址:https://www.cnblogs.com/Zhouzg-2018/p/9692120.html
时间: 2024-10-30 13:20:26