.format字符串拼接
# -*- coding:utf8 -*- #不一一对应会报错
tp1 = "i am {}, age {}, {}".format("charon",18,"pluto")
print(tp1) tp2 = "i am {2}, age {0}, {1}".format("charon",18,"pluto")
print(tp2) tp2 = "i am {0}, age {0}, {1}".format("charon",18,"pluto")
print(tp2) tp3 = "i am {0}, age {0},".format("charon",18,"pluto")
print(tp3) tp4 = "i am {name}, age {age}, {name}".format(name="charon",age=19)
print(tp4) tp5 = "i am {name}, age {age}, {name}".format(**{"name":"charon","age":90})
print(tp5) tp6 = "i am {0[0]}, age {0[1]}, {0[2]}".format([1,2,3],[4,5,6])
print(tp6) tp7 = "i am {:s}, age {:d}, {:f}".format("name",89,56.99)
print(tp7)
#变成字典加两个*
tp8 = "i am {name}, age {age}, {name}".format(**{"name":"charon","age":90})
print(tp8)
#变成列表加一个*
tp9 = "i am {0}, age {1}".format(*["seven", 18])
print(tp9) tp10 = "i am {name:s}, age {age:d}".format(name="seven", age=180)
print(tp10) tp11 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
print(tp11) 结果:
i am charon, age 18, pluto
i am pluto, age charon, 18
i am charon, age charon, 18
i am charon, age charon,
i am charon, age 19, charon
i am charon, age 90, charon
i am 1, age 2, 3
i am name, age 89, 56.990000
i am charon, age 90, charon
i am seven, age 18
i am seven, age 180
i am seven, age 18