Question

Thomas Krüger thomas.krueger at gmx.net
Mon Aug 9 14:07:28 EDT 2004


Dag Hansteen wrote:

>>>> s = "2+2"
>>>> answer = int(str(s))
> Traceback (most recent call last):
>   File "<pyshell#64>", line 1, in ?
>     answer = int(str(s))
> ValueError: invalid literal for int(): 2+2
> 
> How do I this ? I have a string with a such expression and I want to
> return the answer in a variable.

eval('2+2')
-> 4

Thomas




More information about the Python-list mailing list