How do I put a variable inside a string?

前端 未结 8 2291
名媛妹妹
名媛妹妹 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:38

    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.

提交回复
热议问题