【问题标题】:How to insert string into a string as a variable?如何将字符串作为变量插入字符串?
【发布时间】:2019-02-08 20:32:02
【问题描述】:

我正在尝试创建一个程序,而我正在尝试做的一件事是将变量添加到这样的字符串中。

EnemyHealth = 0  

EnemyIs = 'dead'

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

但每次我尝试它都不会让我使用字符串作为变量之一。如何将变量字符串插入另一个字符串?

附:我正在使用 python 3.7.0

【问题讨论】:

    标签: python string python-3.x variables


    【解决方案1】:

    将 print 与 format 一起使用,它会使您的代码看起来不错

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

    【讨论】:

      【解决方案2】:

      我知道有 4 种方法可以在 python (Python 3) 上实现这一点:

      1) 串联:

      您可以使用+ 来连接两个字符串,但是您只能连接字符串类型的数据,这意味着需要使用str() 函数将非字符串类型的数据转换为字符串。例如:

      print("The Enemy's health is " + str(EnemyHealth) + ". The Enemy is " + EnemyIs + ".") 
      # output = "The Enemy's health is 0. The Enemy is dead." 
      

      2) 利用 Python 3 的 print() 函数,该函数可以接受多个参数参数:

      这里你的print() 语句类似于上面的连接语句,除非我使用+ 连接你需要用, 替换它。使用它的好处是,不同的数据类型将自动转换为字符串,即不再需要str() 函数。例如:

      print("The Enemy's health is ", EnemyHealth, ". The Enemy is ", EnemyIs, ".") 
      # output = "The Enemy's health is 0. The Enemy is dead." 
      

      3) 使用字符串替换方法

      您的代码不起作用的原因是因为%d 意味着您将用整数替换它,因此要使您的代码正常工作,因为存储在变量EnemyIs 上的字符串需要替换为%s用于声明它将被替换为字符串。因此,要解决您的问题,您需要这样做:

      print("The Enemy's health is %d. The Enemy is %s." % (EnemyHealth, EnemyIs)) 
      # output = "The Enemy's health is 0. The Enemy is dead." 
      

      4)python字符串的format()方法(这是最好的使用方法)

      这是python中所有字符串的内置方法,它允许您轻松地将python字符串中的占位符{}替换为任何变量。与上面的解决方案 3 不同,此 format() 方法不需要您将不同的数据类型表示或转换为字符串,因为它会自动为您执行此操作。例如,要使您的打印语句起作用,您可以这样做:

      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)) 
      
      # output = "The Enemy's health is 0. The Enemy is dead." 
      

      更新:

      5) F-Strings

      从 python 3.6+ 开始,您现在还可以使用 f 字符串来替换字符串中的变量。该方法类似于上述.format() 方法,在我看来是对str.format() 方法的更好升级。要使用此方法,您只需在定义字符串时在开场引号之前声明f,然后在字符串中使用格式{[variable name goes here]} 即可。例如,要使您的打印语句起作用,使用此方法,您可以:

      print(f"The Enemy's health is {EnemyHealth}. The Enemy is {EnemyIs}.")
      # output = "The Enemy's health is 0. The Enemy is dead." 
      

      正如您使用此方法所见,variable name 直接在大括号 {} 内实例化。

      【讨论】:

      • 谢谢。我没有意识到 %d 和 %s 是不同的。
      • @PLP123 没问题,但是可以,请改用str.format() 方法,因为它不仅更简洁,而且还省去了将不同数据类型转换为字符串的麻烦.
      【解决方案3】:

      避免在 Python3.x 中使用 % 格式说明符

      来自文档:

      旧的格式最终会从语言中删除,通常应该使用 str.format()。

      有几种简单的方法可以做到这一点,请查看以下代码:

      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)
      

      【讨论】:

      • 如果您解释了为什么有人应该避免在python 3.x中进行%替换,这个答案会更好。
      • @BryanOakley 更新已添加
      【解决方案4】:

      您也可以使用print("The Enemy's health is %d. The Enemy is %s." % (EnemyHelath, EnemyIs)),但建议使用print("The Enemy's health is {}. The Enemy is {}.".format(EnemyHealth,EnemyIs))

      【讨论】:

        【解决方案5】:

        字符串对象的格式说明符是%s。所以你的代码应该是:

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

        【讨论】:

          【解决方案6】:

          这是因为%d 在您的第一个字符串中需要一个整数变量。你想要的是 %s 字符串。

          即:

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

          (还请记住,%f 用于浮点数,您可以使用一些技巧,例如 %2.4f 将数字 1.234567 写为 01.2346)

          【讨论】:

          • 我没有意识到它们有不同的用途。谢谢。
          【解决方案7】:

          您的代码的问题是您使用%d 作为第二个参数。 %d 用于整数,而%s 用于字符串。您可以通过更改为%s 来使其工作,如下所示:

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

          输出

          The Enemy's health is 0. The Enemy is dead.
          

          另一种方法是使用如下格式函数:

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

          有关字符串格式的更多信息,请参阅here

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-03-18
            • 1970-01-01
            • 2021-12-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多