我想int加入一个string.这就是我现在正在做的事情:
num = 40
plot.savefig('hanning40.pdf') #problem line
Run Code Online (Sandbox Code Playgroud)
我必须运行几个不同数字的程序,而不是两个40.所以我想做一个循环但插入这样的变量不起作用:
plot.savefig('hanning', num, '.pdf')
Run Code Online (Sandbox Code Playgroud)
如何将变量插入Python字符串?
如果我尝试执行以下操作:
things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)
我在Python 3.x中收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)
......以及Python 2.x中的类似错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?