[英]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.