python中字符串格式化的四种方法
Posted 飘零0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于python中字符串格式化的四种方法的主要内容,如果未能解决你的问题,请参考以下文章