- 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

2nd Mar 2019, 11:51 AM
SlowFast
SlowFast - avatar
7 odpowiedzi
+ 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
2nd Mar 2019, 1:02 PM
Russ
Russ - avatar
+ 1
Use the code as given below. Given var =3 print(str(var))
2nd Mar 2019, 3:56 PM
ikonne Bethel
+ 1
ikon beth you appear to have demonstrated the same misunderstanding of the question as those who downvoted it.
2nd Mar 2019, 4:05 PM
Russ
Russ - avatar
0
Waa thanks
2nd Mar 2019, 1:13 PM
SlowFast
SlowFast - avatar
0
Russ sorry for the misunderstanding. Pls make me understand
2nd Mar 2019, 4:07 PM
ikonne Bethel
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?
2nd Mar 2019, 4:09 PM
Russ
Russ - avatar
0
Now i get it using dictionary format
2nd Mar 2019, 4:15 PM
ikonne Bethel