我想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字符串?
在Python中,编写它是很繁琐的:
print "foo is" + bar + '.'
Run Code Online (Sandbox Code Playgroud)
我可以在Python中做这样的事吗?
print "foo is #{bar}."
ruby python language-comparisons string-formatting string-interpolation
我想做一些类似的String.Format("[{0}, {1}, {2}]", 1, 2, 3)回报:
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我如何在Python中执行此操作?
我必须将代码从 Python2.x 转换为 Python3 (主要是字符串格式)我遇到了这样的事情:
Logger.info("random String %d and %i".format(value1, value2))
Run Code Online (Sandbox Code Playgroud)
现在,我知道 %d 可以替换为 {:d} 但无法使用 {:i} 找到与 %i (带符号)等效的内容,并出现以下错误:
ValueError:类型“int”的对象的未知格式代码“i”