- 2
Anyone knows how to print a variable from a string? For exemple: var = 3, string = "var". Then, you have to print it from string
7 Respostas
+ 2
var = 3
string = âvarâ
print(eval(string)) #3
Use of eval() is disadvised though is itâs vulnerable to attacks if you take a user input. Try and use a dictionary.
var = 3
string = âvarâ
my_dict = {âvarâ: var}
pring(my_dict[string]) #3
+ 1
Use the code as given below.
Given var =3
print(str(var))
+ 1
ikon beth you appear to have demonstrated the same misunderstanding of the question as those who downvoted it.
0
Waa thanks
0
Russ sorry for the misunderstanding. Pls make me understand
0
See my answer above.
To put it another way. If your variable string = âvarâ, and your variable var = 3, how do you print the value of the variable var only from using the variable string?
0
Now i get it
using dictionary format