You could use sympy
module that does necessary formatting for you. It supports many formats such as ascii, unicode, latex, mathml, etc:
from sympy import pretty_print as pp, latex
from sympy.abc import a, b, n
expr = (a*b)**n
pp(expr) # default
pp(expr, use_unicode=True)
print(latex(expr))
print(expr.evalf(subs=dict(a=2,b=4,n=5)))
Output
n
(a*b)
n
(a?b)
$left(a b
ight)^{n}$
32768.0000000000
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…