占位符用 %s 代表字符串 ,%d 代表整数:
>>> a="this is %s %s"%('my','apple')
>>> print a
>>> this is my apple
format()为字符串内置方法,它的占位符用 {} 代替,传入对应参数即可:
>>> b="this is {} {}".format('my','apple')
>>> print b
>>> this is my apple
format()也可以在 {} 中放参数位置,这样则可以打乱后面参数的顺序:
>>> b="this is {1} {0}".format('apple','my')
>>> print b
>>> this is my apple
format()也可以用字典方式,如:
>>> b="this is {whose} {fruit}".format(fruit='apple',whose='my')
>>> print b
>>> this is my apple
>>> b="this is %(whose)s %(fruit)s" % ('fruit':'apple','whose':'my')
>>> print b
>>> this is my apple
占位符 && format
最新推荐文章于 2024-04-19 13:40:58 发布