如何将变量放在字符串中?

问题:如何将变量放在字符串中?

我想int放入一个string。这是我目前正在做的事情:

num = 40
plot.savefig('hanning40.pdf') #problem line

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

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

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

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

num = 40
plot.savefig('hanning40.pdf') #problem line

I have to run the program for several different numbers, so I’d like to do a loop. But inserting the variable like this doesn’t work:

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

How do I insert a variable into a Python string?


回答 0

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

%运营商,下面的字符串时,允许你插入值到通过格式代码的字符串(%d在这种情况下)。有关更多详细信息,请参见Python文档:

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

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:

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


回答 1

哦,很多很多方式…

字符串串联:

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

转换说明符:

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

使用局部变量名:

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

使用str.format()

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

使用f字符串:

plot.savefig(f'hanning{num}.pdf') # added in Python 3.6

使用string.Template

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

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 str.format():

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

Using f-strings:

plot.savefig(f'hanning{num}.pdf') # added in Python 3.6

Using string.Template:

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

回答 2

通过在Python 3.6中引入格式化的字符串文字(简称为“ f-strings”),现在可以使用更简短的语法编写该文字了:

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

通过问题中给出的示例,它看起来像这样

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

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:

>>> 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')

回答 3

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

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

希望有帮助!

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().

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

Hope that helps!


回答 4

通常,您可以使用以下命令创建字符串:

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

In general, you can create strings using:

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

回答 5

如果您想将多个值放入字符串中,则可以使用 format

nums = [1,2,3]
plot.savefig('hanning{0}{1}{2}.pdf'.format(*nums))

将导致字符串hanning123.pdf。可以使用任何数组来完成。

If you would want to put multiple values into the string you could make use of format

nums = [1,2,3]
plot.savefig('hanning{0}{1}{2}.pdf'.format(*nums))

Would result in the string hanning123.pdf. This can be done with any array.


回答 6

我需要一个扩展版本:我不需要在字符串中嵌入单个数字,而是需要生成一系列格式为’file1.pdf’,’file2.pdf’等的文件名。这就是它的方式工作:

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

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:

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

回答 7

您只需要使用以下命令将num变量转换为字符串

str(num)

You just have to cast the num varriable into a string using

str(num)