How do I put a variable inside a string?

前端 未结 8 2285
名媛妹妹
名媛妹妹 2020-11-21 06:05

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          


        
8条回答
  •  时光说笑
    2020-11-21 06:33

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

提交回复
热议问题