如何将字符串作为变量插入字符串? [英] How to insert string into a string as a variable?

查看:59
本文介绍了如何将字符串作为变量插入字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 Enemy's health is %d. The Enemy is %d." % (EnemyHealth,EnemyIs)

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

But everytime I try that it won't let me use a string as one of the variables. How would I insert a variable string into another string?

附言我正在使用 python 3.7.0

P.S. I am using python 3.7.0

推荐答案

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

There are 4 approaches I am aware of for achieving this on python (Python 3):

1) 串联:

您可以使用 + 来连接 2 个字符串,但是您只能连接字符串类型的数据,这意味着需要使用 str()<将非字符串类型的数据转换为字符串/代码> 功能.例如:

You can do this using + for joining 2 strings, however you can only concatenate string type data, meaning non string type data needs to be converted to string using the str() function. For example:

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() 函数,它可以接受多个参数:

2) Taking advantage of Python 3's print() function which can take multiple parameter arguments:

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

Here your print() statement similar to the concatenation statement above, except where ever I use + to concatenate you need to replace it with ,. The advantage of using this, is that different data types will be automatically converted to string, i.e. no longer needing the str() function. For example:

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 ,用于声明它将被替换为字符串.因此,要解决您的问题,您需要执行以下操作:

The reason your code doesnt work, is because %d means you will replace it with an integer and therefore to make your code work, for the string stored on the variable EnemyIs needs to be replaced with %s which is used to state that it will be replaced with a string. Therefore to solve your problem you need to do:

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()方法(这是最好的使用方法)

4) The format() method for python strings (This is the best approach to use)

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

This is a built in method for all strings in python, which allow you to easily replace the placehoder {} within python strings, with any variables. Unlike solution number 3 above, this format() method doesnt require you to express or convert different data types to string, as it would automatically do this for you. For example, to make your print statement work you can do:

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-strings 来替换字符串中的变量.这个方法类似于上面描述的 .format() 方法,在我看来是对 str.format() 方法的更好的升级.要使用此方法,您只需在定义字符串时在起始引号之前声明 f ,然后在字符串中使用格式 {[variable name go here]} 使其工作.例如,要使您的打印语句起作用,使用此方法,您可以执行以下操作:

As of python 3.6+, you can now also use f-strings, to substitute variables within a string. This method is similar to the the .format() method described above, and in my oppinion is a better upgrade to the str.format() method. To use this method all you have to do is state f before your opening quote when defining a string and then, within the string use the format, {[variable name goes here]} for this to work. For example, to make your print statement work, using this method, you can do:

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

如您所见,通过使用此方法,变量名 直接在大括号 {} 内实例化.

As you can see by using this method, the variable name is being instanciated directly within the curly brackets {}.

这篇关于如何将字符串作为变量插入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆