如何在Python中放置一个变量?

时间:2022-02-14 20:11:52

I would like to put an int into a string. This is what I am doing at the moment:

我想把一个int数变成一个字符串。这就是我现在正在做的事情:

end = smooth(data,window_len=40)
plot.plot(time[0:len(end)],end)
plot.savefig('hanning(40).pdf') #problem line

I have to run the program for several different numbers instead of the two 40's. So I'd like to do a loop but inserting the variable like this doesn't work:

我必须为几个不同的数字而不是两个40运行这个程序。我想做一个循环,但是插入这样的变量不起作用:

plot.savefig('hanning',num,'.pdf')

How do I insert a variable into a Python string?

如何将变量插入到Python字符串中?

6 个解决方案

#1


115  

plot.savefig('hanning(%d).pdf' % num)

The % operator, when following a string, allows you to insert values into that string via format codes (the %d in this case). For more details, see the Python documentation:

当遵循字符串时,%操作符允许您通过格式代码(本例中为%d)将值插入该字符串。有关详细信息,请参阅Python文档:

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

https://docs.python.org/3/library/stdtypes.html printf-style-string-formatting

#2


257  

Oh, the many, many ways...

哦,有很多很多方法……

String concatenation:

字符串连接:

plot.savefig('hanning' + str(num) + '.pdf')

Conversion Specifier:

转换说明符:

plot.savefig('hanning%s.pdf' % num)

Using local variable names:

使用局部变量的名字:

plot.savefig('hanning%(num)s.pdf' % locals()) # Neat trick

Using format():

使用格式():

plot.savefig('hanning{0}.pdf'.format(num)) # Note: This is the new preferred way

Using string.Template:

使用均:

plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))

#3


50  

With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is now possible to write this with a briefer syntax:

随着Python 3.6中引入格式化字符串(简称“f-string”),现在可以使用更简洁的语法来编写它:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

With the example given in the question, it would look like this

根据问题中给出的例子,它应该是这样的

plot.savefig(f'hanning{num}.pdf')

#4


10  

Not sure exactly what all the code you posted does, but to answer the question posed in the title, you can use + as the normal string concat function as well as str().

不确定所发布的所有代码都是做什么的,但是要回答标题中提出的问题,可以使用+作为常规字符串concat函数和str()。

"hello " + str(10) + " world" = "hello 10 world"

Hope that helps!

希望会有帮助!

#5


4  

In general, you can create strings using:

一般来说,您可以使用以下方法创建字符串:

stringExample = "someString " + str(someNumber)
print(stringExample)
plot.savefig(stringExample)

#6


2  

I had a need for an extended version of this: instead of embedding a single number in a string, I needed to generate a series of file names of the form 'file1.pdf', 'file2.pdf' etc. This is how it worked:

我需要一个扩展版本:不需要在字符串中嵌入单个数字,而是需要生成一系列表单“file1”的文件名。pdf”、“file2。pdf等。这是它的工作原理:

['file' + str(i) + '.pdf' for i in range(1,4)]

#1


115  

plot.savefig('hanning(%d).pdf' % num)

The % operator, when following a string, allows you to insert values into that string via format codes (the %d in this case). For more details, see the Python documentation:

当遵循字符串时,%操作符允许您通过格式代码(本例中为%d)将值插入该字符串。有关详细信息,请参阅Python文档:

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

https://docs.python.org/3/library/stdtypes.html printf-style-string-formatting

#2


257  

Oh, the many, many ways...

哦,有很多很多方法……

String concatenation:

字符串连接:

plot.savefig('hanning' + str(num) + '.pdf')

Conversion Specifier:

转换说明符:

plot.savefig('hanning%s.pdf' % num)

Using local variable names:

使用局部变量的名字:

plot.savefig('hanning%(num)s.pdf' % locals()) # Neat trick

Using format():

使用格式():

plot.savefig('hanning{0}.pdf'.format(num)) # Note: This is the new preferred way

Using string.Template:

使用均:

plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))

#3


50  

With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is now possible to write this with a briefer syntax:

随着Python 3.6中引入格式化字符串(简称“f-string”),现在可以使用更简洁的语法来编写它:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

With the example given in the question, it would look like this

根据问题中给出的例子,它应该是这样的

plot.savefig(f'hanning{num}.pdf')

#4


10  

Not sure exactly what all the code you posted does, but to answer the question posed in the title, you can use + as the normal string concat function as well as str().

不确定所发布的所有代码都是做什么的,但是要回答标题中提出的问题,可以使用+作为常规字符串concat函数和str()。

"hello " + str(10) + " world" = "hello 10 world"

Hope that helps!

希望会有帮助!

#5


4  

In general, you can create strings using:

一般来说,您可以使用以下方法创建字符串:

stringExample = "someString " + str(someNumber)
print(stringExample)
plot.savefig(stringExample)

#6


2  

I had a need for an extended version of this: instead of embedding a single number in a string, I needed to generate a series of file names of the form 'file1.pdf', 'file2.pdf' etc. This is how it worked:

我需要一个扩展版本:不需要在字符串中嵌入单个数字,而是需要生成一系列表单“file1”的文件名。pdf”、“file2。pdf等。这是它的工作原理:

['file' + str(i) + '.pdf' for i in range(1,4)]