16
字符串格式:%与.format
Python 2.6引入的str.format()方法与现有%运算符的语法略有不同。哪个更好,什么情况下适合? 以下使用每种方法并具有相同的结果,那么有什么区别? #!/usr/bin/python sub1 = "python string!" sub2 = "an arg" a = "i am a %s" % sub1 b = "i am a {0}".format(sub1) c = "with %(kwarg)s!" % {'kwarg':sub2} d = "with {kwarg}!".format(kwarg=sub2) print a # "i am a python string!" print b # "i am a …