How to change priority in math order(asterisk)
<p>I want users to input math formula in my system. How can convert case1 formula to case2 formula using Python? In another word, I would like to change math order specifically for double asterisks.</p>

<pre><code>#case1
3*2**3**2*5
&gt;&gt;&gt;7680

#case2
3*(2**3)**2*5
&gt;&gt;&gt;960
</code></pre>

<p>Below is the example to achieve this using <code>re</code> as:</p>

<pre><code>expression = '3*2**3**2*5'
asterisk_exprs = re.findall(r"\d+\*\*\d+", expression) # List of all expressions matching the criterion 
for expr in asterisk_exprs:
    expression = expression.replace(expr, "({})".format(expr)) # Replace the expression with required expression

# Value of variable 'expression': '3*(2**3)**2*5'
</code></pre>

<p>In order to evaluate the mathematical value of <code>str</code> expression, use <code>eval</code> as:</p>

<pre><code>&gt;&gt;&gt; eval(expression) 
960
</code></pre>

python python-2.7 numpy math