How to get the value of a variable through a string?
Example:
dog = 1
cat = 2
animal = 'dog'
What to do to transform the animal
variable into 1
?
How to get the value of a variable through a string?
Example:
dog = 1
cat = 2
animal = 'dog'
What to do to transform the animal
variable into 1
?
Use eval
function:
eval(animal) # =1
One option is eval
, which comes with the caveat that you should never use it with user-supplied input because they could execute arbitrary commands.