python中字符串格式化的四种方法

时间:2022-06-27 07:23:13
 name = "huangemiling"
age= 10
address = 'nanjing'
print("My name is %s,age is %d,I come from %s"%(name,age,address))
print("My name is {0},age is {1},I come from {2}".format(name,age,address))
print("My name is {:s},age is {:d},I come from {:s}".format(name,age,address))
print("My name is {name},age is {age},I come from {address}".\
format(name=name,age=age,address=address))

对应的结果如下:

My name is huangemiling,age is 10,I come from nanjing
My name is huangemiling,age is 10,I come from nanjing
My name is huangemiling,age is 10,I come from nanjing
My name is huangemiling,age is 10,I come from nanjing