Turning a string into an programatic mathematical expression

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Oct 20 06:21:52 CEST 2004

"Daniel Bickett" <dbickett at gmail.com> wrote in message
news:mailman.5204.1098239457.5135.python-list at python.org...
> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
> for your time :)
> Daniel Bickett

This is a pretty standard text processing task, often assigned as homework
in CS classes.  Check out this entry from the Python Tutor list
http://mail.python.org/pipermail/tutor/2003-December/027032.html, authored
by Danny Yoo, which includes many helpful points about this problem.

You can also find a working Python expression parser included with the
examples provided with the pyparsing parser module, to be found at
http://pyparsing.sourceforge.net .

-- Paul McGuire

More information about the Python-list mailing list