- Related Questions & Answers
- How can we do date and time math in Python?
- How to do Python math at command line?
- How To Do Math With Lists in python ?
- How to force JavaScript to do math instead of putting two strings together?
- Math Operations on BigInteger in Java
- MongoDB aggregation / math operation to sum score of a specific student
- How can we concatenate two strings using jQuery?
- How can we do Python operator overloading with multiple operands?
- What does % do to strings in Python?
- How we can run Python CGI Script on Windows XP?
- 8085 Program to do an operation on two numbers based on the contents of X
- Python set operation.
- How can we do the basic print formatting for Python numbers?
- Can we use Switch statement with Strings in java?
- 8085 Program to do an operation on two BCD numbers based on the contents of X

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

You can use the eval function to evaluate mathematical expressions in strings.

For example, if you have a string with the content (4*5) + 21, you can eval it and get the result.

>>> s = "(4*5) + 22" >>> eval(s) 42

Eval follows Python rules for evaluating mathematical expressions for cases in which parenthesis are not provided, etc. Be very careful when using eval though as it can be a source of huge security loopholes and bugs.

Advertisements