So I want to convert a string to a math in python but am getting this error
string = "2 + 5"
math = int(string)
print(math)
>>> Should output 7 but instead outputs an error
How do I do it?
>Solution :
Instead of int() use eval():
string = "2 + 5"
math = eval(string)
print(math)
Or simply:
string = "2 + 5"
print(eval(string))