You are looking for eval:
>>> s = '5 + 7' >>> eval(s) 12
Be careful not to run it on untrusted code, since an attacker can use it to run arbitrary code on your system. For example, if the user can make s equal to "__import__('os').execve('/bin/sh',[],{})" , then eval(s) will provide the user with a shell on your computer.
source share