How to insert string into a string as a variable?

前端 未结 7 550
野的像风
野的像风 2021-01-06 09:25

I\'m trying to create a program and one thing I\'m trying to do is add variables to a string like so.

EnemyHealth = 0  

EnemyIs = \'dead\'

print(\"The Enem         


        
7条回答
  •  半阙折子戏
    2021-01-06 10:19

    That's because %d in your first string expects an integer variable in its place. What you want is %s for strings.

    That is:

    print("The Enemy's health is %d. The Enemy is %s." % (EnemyHealth,EnemyIs)
    

    (Also have in mind that %f is for floats and you can use some tricks like %2.4f will write number 1.234567 as 01.2346)

提交回复
热议问题