python中字符串格式化的四种方法
1 name = "huangemiling" 2 age= 10 3 address = 'nanjing' 4 print("My name is %s,age is %d,I come from %s"%(name,age,address)) 5 print("My name is {0},age is {1},I come from {2}".format(name,age,address)) 6 print("My name is {:s},age is {:d},I come from {:s}".format(name,age,address)) 7 print("My name is {name},age is {age},I come from {address}".\ 8 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