python 中字符串格式化函数 format()
作者:互联网
001、
>>> "{0}".format("xxx") ## 位置参数 'xxx' >>> "{0}.{1}.{2}".format("xxx", "yyy", "zzz") 'xxx.yyy.zzz' >>> "\t{0}.{1}.{2}".format("xxx", "yyy", "zzz") '\txxx.yyy.zzz' >>> "{0}.\n{1}.{2}".format("xxx", "yyy", "zzz") 'xxx.\nyyy.zzz' >>> print("{0}.\n{1}.{2}".format("xxx", "yyy", "zzz")) xxx. yyy.zzz
002、
>>> "{a}.{b}.{c}".format(a = "aaa", b = "bbb", c = "ccc") ## 关键字参数 'aaa.bbb.ccc' >>> "{a}.{b}.{c}".format(a = "ccc", b = "bbb", c = "aaaa") 'ccc.bbb.aaaa' >>> "\t{a}.{b}.{c}".format(a = "ccc", b = "bbb", c = "aaaa") '\tccc.bbb.aaaa' >>> print("\t{a}.{b}.{c}".format(a = "ccc", b = "bbb", c = "aaaa")) ccc.bbb.aaaa
当同时出现位置参数和关键字参数时, 位置参数应写在关键字参数的前面。
标签:格式化,zzz,format,python,xxx,yyy,bbb,ccc 来源: https://www.cnblogs.com/liujiaxin2018/p/16585349.html