If I have a string like this
"value = 10"
how would I make it so the value is equal to 10
Asked
Active
Viewed 90 times
0

mlghacker xX
- 29
- 6
-
Hey there, this might help answer your question: https://stackoverflow.com/questions/43878504/how-can-i-convert-string-to-source-code-in-python – Keverly Apr 12 '21 at 16:42
-
@Anurag, I believe he wants `10` being assigned to the variable `value` – Martí Apr 12 '21 at 16:44
-
Well then just do `value = int("value = 10".split(' ')[-1])` – Have a nice day Apr 12 '21 at 16:45
-
Btw `eval()` method doesn't work in this case It throws error...you can also use `value=int("value = 10"[8:])` – Anurag Dabas Apr 12 '21 at 16:46
-
You're right @Anurag, I'll remove my answer... I would have sweared that worked though... – Martí Apr 12 '21 at 16:48
2 Answers
0
>>> exec("value = 10")
>>> print(value)
10
>>> exec("value = 11")
>>> print(value)
11

Marcel Preda
- 1,045
- 4
- 18