How to insert string into a string as a variable?

前端 未结 7 544
野的像风
野的像风 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:18

    Avoid % format specifiers in Python3.x

    From the Docs :

    old style of formatting will eventually be removed from the language, str.format() should generally be used.

    There are few simple ways to do this, check below code :

    print("The Enemy's health is {} The Enemy is {}".format(EnemyHealth, EnemyIs)
    
    print("The Enemy's health is {0} The Enemy is {1}".format(EnemyHealth, EnemyIs)
    
    print("The Enemy's health is {EnemyHealth} The Enemy is {EnemyIs}".format(EnemyHealth=EnemyHealth, EnemyIs=EnemyIs)
    

提交回复
热议问题