-2

Say I have a variable named variable and make it equal to a string that I would like to print out. Now, let’s say I only have a string version of the variable name. How could I print what the variable is? Here’s what I mean:

variable = "Hello!"
string = "variable"

How could I make it print "Hello" from the string?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
raid6n
  • 55
  • 1
  • 7

1 Answers1

3

To take your example.

variable = 'variable'

So, we just have now the value of variable, which is 'variable'.

But, if we type

globals()['variable']

OUTPUT

enter image description here

It's exactly as if you had typed variable

Lumber Jack
  • 602
  • 3
  • 9