How do I put a variable inside a string?

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

    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!

    0 讨论(0)
  • 2020-11-21 06:44

    In general, you can create strings using:

    stringExample = "someString " + str(someNumber)
    print(stringExample)
    plot.savefig(stringExample)
    
    0 讨论(0)
提交回复
热议问题