【发布时间】:2011-10-23 12:28:14
【问题描述】:
我现在正在学习 Python,耶!无论如何,我有一个小问题。我在这里没有看到问题:
x = 3
y = 7
z = 2
print "I told to the Python, that the first variable is %d!" % x
print "Anyway, 2nd and 3rd variables sum is %d. :)" % y + z
但 Python 的想法不同 - TypeError: cannot concatenate 'str' and 'int' objects。
为什么会这样?我没有将任何变量设置为字符串……就我所见。
【问题讨论】:
-
请注意,在当前版本的 Python 中进行字符串格式化的推荐方法是
str.format()函数。"Anyway, 2nd and 3rd variables sum is {0}. :)".format(y+z)(0在 Python 2.7+ 中可以省略,在这种情况下占位符只是{}。)
标签: python string integer concatenation typeerror