你如何在 Python 中打印上标? [英] How do you print superscript in Python?
问题描述
我知道 python 中的 xb 函数,但它似乎对我不起作用.我知道我可能需要下载第三方模块来完成此操作,如果需要,哪个最好?
I am aware of the xb function in python, but it does not seem to work for me. I am aware that I may need to download a third party module to accomplish this, if so, which one would be best?
我目前正在编写一个二项式展开求解器,以尝试使用我自学的技能.当我尝试显示用户输入的扩展以供确认时出现问题.目前我不得不像这样打印表达式:
I am currently writing a binomial expansion solver, to try and use skills which I am teaching myself. The problem arises when I attempt to display the user input-ed expansion to the use for confirmation. Currently I am having to print the expression like so:
var1 = input("Enter a: ")
var2 = input("Enter b: ")
exponent = input("Enter n: ")
a = int(var1)
b = int(var2)
n = int(exponent)
expression = ('(%(1)dx%(2)d)^%(3)d') %
{'1' : a, '2' : b, '3' : n}
print(expression)
confirmation = input(str("Is this correctt? Y/N "))
这会打印 (2x4)^5,而我更喜欢将索引打印为上标.这怎么办?
This prints (2x4)^5, whereas I'd prefer the index to be printed as superscript. How can this be done?
推荐答案
您可以使用 sympy
模块为您进行必要的格式化.支持ascii、unicode、latex、mathml等多种格式:
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)))
输出
n
(a*b)
n
(a⋅b)
$left(a b
ight)^{n}$
32768.0000000000
这篇关于你如何在 Python 中打印上标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!