For a solution without using eval, here's what I would do. Start by finding all of the mathematical expressions in the string, which I will define as a string that contains spaces, parenthesis, numbers, and operations, then strip out the matches that are all whitespace:
>>> import re
>>> my_str = 'I have 6 * (2 + 3) apples'
>>> exprs = list(re.finditer(r"[\d\.\s\*\+\-\/\(\)]+", my_str))
>>> exprs = [e for e in exprs if len(my_str[e.start():e.end()].strip()) > 0]
Next, evaluate the expressions using the NumericStringParser
class from this question, which uses pyparsing:
>>> nsp = NumericStringParser()
>>> results = [nsp.eval(my_str[e.start():e.end()]) for e in exprs]
>>> results
[30.0]
Then, to substitute the results back into the expression, reverse sort the expressions by their starting index and place them back into the original string:
>>> new_str = my_str
>>> for expr, res in sorted(zip(exprs, results), key=lambda t: t[0].start(), reverse=True):
... new_str = new_str[:expr.start()] + (" %d " % res) + new_str[expr.end():]
...
>>> new_str
'I have 30 apples'
'I have {{ 6 * (2 + 3) }} apples'
as an input format? – Puddingeval()
function: codepad.org/QOhSpWzH – Waterfowl