stringExp = \"2^4\"
intVal = int(stringExp) # Expected value: 16
This returns the following error:
Traceback (most recent call
I think I would use eval()
, but would first check to make sure the string is a valid mathematical expression, as opposed to something malicious. You could use a regex for the validation.
eval()
also takes additional arguments which you can use to restrict the namespace it operates in for greater security.