python中上标星号怎么打,如何在Python中打印上标?

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 a noob with Python, and with StackOverflow hence my basic question. Now a bit about the context...

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 "))

This prints (2x4)^5, whereas I'd prefer the index to be printed as superscript. How can this be done? I will provide any (missing) information if needed.

解决方案

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\right)^{n}$

32768.0000000000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值