1)%格式化方法
>>> a = "this is %s %s" % ("my", "apple") >>> a 'this is my apple'
2)内置方法format():
>>> a = "this is {} {}".format("apple","my") >>> a 'this is apple my' >>> a = "this is {1} {0}".format("apple","my") //添加标识,可以控制顺序 >>> a 'this is my apple' >>> a = "this is {whose} {fruit}".format(fruit = "apple", whose = "my") //添加有意义的标识,使代码更加友好 >>> a 'this is my apple'
3)使用字典的方式:
>>> a = "this is %(whose)s %(fruit)s" % {"whose":"my", "fruit":"apple"} //注意:字典用的是{} >>> a 'this is my apple'
#学习笔记,如有谬误,敬请指正。#