[Tutor] Evaluating a string expression

Serdar Tumgoren zstumgoren at gmail.com
Thu Nov 5 14:21:15 CET 2009


> I would like to know how would I evaluate a string expression in python.
> For example, if i say:
>>>> a = "3*2"
> I want to do something to evaluate the variable 'a' to give me 6. How
> can I do this?
>

I think the "eval" built-in function is what you're after:

>>> a = "3*2"
>>> eval(a)
6

http://docs.python.org/library/functions.html


More information about the Tutor mailing list