How do I put a variable inside a string?

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

    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

提交回复
热议问题