3 回答

TA贡献1828条经验 获得超3个赞
您可以使用sympy为您执行必要格式化的模块。它支持多种格式,例如ascii,unicode,latex,mathml等:
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)))
输出量
n
(a*b)
n
(a⋅b)
$\left(a b\right)^{n}$
32768.0000000000

TA贡献1807条经验 获得超9个赞
您需要使用“格式”类型的东西。使用{}\u00b2".format(area))" and the{} becomes a²`。这是一个例子:
print("The area of your rectangle is {}cm\u00b2".format(area))
代码的结尾将打印出来cm²。您可以将大号2更改为其他数字以得到不同的结果。我不知道如何下标。

TA贡献1816条经验 获得超6个赞
在Python 3.6+中(仅因为示例使用了先前版本中没有的f字符串而提及),命名为Unicode字符提供了一种易于编写,易于阅读的方式来执行此操作。这是清单。
例:
f'\N{GREEK SMALL LETTER GAMMA}={density:.2f} t/m\N{SUPERSCRIPT THREE}'
产生类似
γ=1.20 t/m³
添加回答
举报