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?
Instead of int()
use eval()
:
string = "2 + 5"
math = eval(string)
print(math)
Or simply:
string = "2 + 5"
print(eval(string))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments