Python 如何打印字符串的值,而不是字符串本身

Python 如何打印字符串的值,而不是字符串本身,python,Python,我有一个可能很简单的问题,我似乎不知道该怎么做。如何做这样的事情: race = goblin #I change the race here goblingrowth = 200 humangrowth = 300 orgegrowth = 400 print (race + "growth") #In this case, it will print the string "goblingrowth" in python, but I want it to print t

我有一个可能很简单的问题,我似乎不知道该怎么做。如何做这样的事情:

race = goblin  #I change the race here


goblingrowth = 200  
humangrowth = 300  
orgegrowth = 400

print (race + "growth")  #In this case, it will print the string "goblingrowth" in 
python, but I want it to print the value of the variable (goblingrowth), 
which is 200, and i have to do it this way. 

非常感谢您提供的任何帮助

您可以这样做的一种方法是访问
locals()
字典,该字典保存代码的局部变量,并从您拥有的字符串中获取变量的值。例如:

race = 'goblin'  #I change the race here

goblingrowth = 200  
humangrowth = 300  
orgegrowth = 400

print(locals()[race + 'growth'])  # this accesses the variable goblingrowth in the locals dictionary
class Living(object):
    def __init__(self, name, growth):
        self.name = name
        self.growth = growth

goblin = Living("goblin", 200)
human  = Living("human", 300)
ogre   = Living("ogre", 400)

for living in (goblin, human, ogre):
    print(living.name + " growth is " + str(living.growth))

将输出
200
。希望这有帮助

一种方法是访问保存代码的局部变量的
locals()
字典,并从您拥有的字符串中获取变量的值。例如:

race = 'goblin'  #I change the race here

goblingrowth = 200  
humangrowth = 300  
orgegrowth = 400

print(locals()[race + 'growth'])  # this accesses the variable goblingrowth in the locals dictionary
class Living(object):
    def __init__(self, name, growth):
        self.name = name
        self.growth = growth

goblin = Living("goblin", 200)
human  = Living("human", 300)
ogre   = Living("ogre", 400)

for living in (goblin, human, ogre):
    print(living.name + " growth is " + str(living.growth))

将输出
200
。希望这有帮助

只需将
goblingrowth
添加到您的打印内容中,我将在下面展示。但是,按照这样做的方式,您必须将变量转换为字符串(因为您的
goblingrowth
是一个int),这不是很理想。您可以这样做:

print(race + " growth " + str(goblingrowth))
但是,我们强烈建议您使用字符串格式,而不是像这样构造输出:

print("{0} growth: {1}".format(race, goblingrowth))
上面发生的事情是,您正在将参数设置到字符串中的每个位置,因此,
{0}
指示您提供用于格式化并设置在该字符串位置的第一个参数,即
race
,然后,
{1}
将指示提供用于格式化的第二个参数,这是goblingrowth。实际上,您不需要提供这些数字,但我建议您阅读下面提供的文档以了解更多信息


阅读有关字符串格式的信息。这将大有帮助

只需将
goblingrowth
添加到您的打印内容中,我将在下面展示。但是,按照这样做的方式,您必须将变量转换为字符串(因为您的
goblingrowth
是一个int),这不是很理想。您可以这样做:

print(race + " growth " + str(goblingrowth))
但是,我们强烈建议您使用字符串格式,而不是像这样构造输出:

print("{0} growth: {1}".format(race, goblingrowth))
上面发生的事情是,您正在将参数设置到字符串中的每个位置,因此,
{0}
指示您提供用于格式化并设置在该字符串位置的第一个参数,即
race
,然后,
{1}
将指示提供用于格式化的第二个参数,这是goblingrowth。实际上,您不需要提供这些数字,但我建议您阅读下面提供的文档以了解更多信息


阅读有关字符串格式的信息。这将大有帮助

更好的方法是使用一个类来表示不同类型的生命实体。然后可以为每个比赛创建一个实例,设置属性。然后,您将可以方便地访问给定生活的所有属性。例如:

race = 'goblin'  #I change the race here

goblingrowth = 200  
humangrowth = 300  
orgegrowth = 400

print(locals()[race + 'growth'])  # this accesses the variable goblingrowth in the locals dictionary
class Living(object):
    def __init__(self, name, growth):
        self.name = name
        self.growth = growth

goblin = Living("goblin", 200)
human  = Living("human", 300)
ogre   = Living("ogre", 400)

for living in (goblin, human, ogre):
    print(living.name + " growth is " + str(living.growth))
这将产生:

goblin growth is 200
human growth is 300
ogre growth is 400

更好的方法是使用一个类来表示不同类型的生命实体。然后可以为每个比赛创建一个实例,设置属性。然后,您将可以方便地访问给定生活的所有属性。例如:

race = 'goblin'  #I change the race here

goblingrowth = 200  
humangrowth = 300  
orgegrowth = 400

print(locals()[race + 'growth'])  # this accesses the variable goblingrowth in the locals dictionary
class Living(object):
    def __init__(self, name, growth):
        self.name = name
        self.growth = growth

goblin = Living("goblin", 200)
human  = Living("human", 300)
ogre   = Living("ogre", 400)

for living in (goblin, human, ogre):
    print(living.name + " growth is " + str(living.growth))
这将产生:

goblin growth is 200
human growth is 300
ogre growth is 400

您可以将这些值存储在字典中,而不是作为单独的变量

growths = {'goblin': 200, 'humans': 300, 'ogre': 400}
print growths[race]

您可以将这些值存储在字典中,而不是作为单独的变量

growths = {'goblin': 200, 'humans': 300, 'ogre': 400}
print growths[race]

我认为有人否决了你,因为使用
eval
确实不是一个好主意。我建议你重新制定你的解决方案。阅读并开始。这不是最好的做法,使用起来很危险。你可以考虑使用<代码> AST。我建议你重新制定你的解决方案。阅读并开始。这不是最好的做法,而且使用起来非常危险。你可以考虑使用<代码> AST。非常感谢所有的答案,我刚刚开始使用python,而且是weeb级别的,这些技术会派上用场的!