How to call variable with input string

1.4k views Asked by At

This is my problem:

I want to make a input that will run the name of the variable that is input.

var = "working!"
cmd = input(":")
#I want it so that the user inputs "var", the var variable ("working!") is printed.
#But if I put the print like it is below, it will print a string of what is input. So, it will print "var".
print(cmd)

Essentially, I want something that turns the input string into a variable name, so I can call said variable.

1

There are 1 answers

1
Mike On

You need to use the eval() function:

var = "working!"
print(eval("var"))