我正在學習Python,耶!無論如何,我有一個小問題。我在這裏沒有看到問題:TypeError:無法連接'str'和'int'對象
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()'函數。 ''無論如何,第二和第三個變量的總和是{0}。:)「。format(y + z)'(在Python 2.7+中可以省略'0',在這種情況下佔位符只是'{}' ) – JAB