Python%运算符vs连接

时间:2016-02-10 05:30:16

标签: python concatenation interpolation

什么时候最好使用:

for n in range(0, 100):
    print "The count is "+str(n)

Vs以上。

for n in range(0, 100):
    print "The count is %d" % (n)

反之亦然?

1 个答案:

答案 0 :(得分:1)

{@ 1}}语法已被弃用。使用%d进行字符串格式化。

示例:

str.format

详细了解"My name is {0}.".format("Austin") "I am {} years old and make ${:.2f} per hour.".format(50, 50.29999) # positional arguments myDict = { "language" : "English", "major" : "Computer Networking" } "I speak {language} and have a degree in {major}.".format(**myDict) myList = [2, "German Sheppard", "Lucy", "Ethel" ] "I own {} {}s named '{}' and '{}'.".format(*myList) here

相关问题