繁体   English   中英

Python%运算符vs串联

[英]Python % operator vs concatenation

什么时候最好使用:

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)

还是反之亦然?

%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)

在此处阅读有关str.format更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM