How do I convert arithemtic string (like "2+2") to a number?

Arvid Andersson arvid at linux.se
Sat Feb 5 17:02:18 EST 2005


Hello, I need to convert a string to a number, but the string can 
contain +,-,* and / as well as parenthesis. For example, if I have the 
string "30/(6+9)" I would like a function that returned the number 2.

I actually wrote a java function that did this a couple of years ago, in 
school, as an excersise in "binary trees". I lost it, and most of my 
programming knowledge, but I figured perhaps there is a way to do this 
easily in python? It seems to me like it could be a common problem.

/Arvid Andersson



More information about the Python-list mailing list