How do you print superscript in Python?

房东的猫 提交于 2019-11-26 17:56:52

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
James

You need to use a 'format' type thing. Use {}\u00b2".format(area))" and the{}becomes a²`. Here is an example:

print("The area of your rectangle is {}cm\u00b2".format(area))

The end of the code will print cm². You can change the large 2 at the end to other numbers for a different result. I do not know how to do a lower subscript though.

In Python 3.6+ (mentioned only because the example uses f-strings that are not available in previous versions) named Unicode characters provide an easy to write, easy to read way to do this. Here is a list.

Example:

f'\N{GREEK SMALL LETTER GAMMA}={density:.2f} t/m\N{SUPERSCRIPT THREE}'

yields something like

γ=1.20 t/m³
ranksrejoined

You're using input(), so I imagine this is console-based. To that end, you have two options, as previously discussed here. One is to use a bit of formatting trickery to display the exponents on the line above the actual expansion. The other is to use these nifty characters, assuming your console supports Unicode:

⁰¹²³⁴⁵⁶⁷⁸⁹

You're probably going to have to increase the font size by quite a bit for them to be legible, but it's certainly a viable option assuming proper support. Aside from that, though, you mentioned this is a personal learning experience; why not combine it with another and learn the simpler aspects of Pygame? It's very straightforward, text manipulation and keyboard events couldn't be simpler, and it's never a wrong step to branch out.

Your Python program is probably running as a console application which can only output characters w/o formatting. The simple answer to your question is "you can't do it".

Of course it is possible to write a GUI application, our output to a document format which supports formatting (RTF, HTML, TeX, etc), but this is most likely beyond the scope of the homework problem you are working on.

As MK already said, you cannot format output on the command line (besides some colors and bold/blinking depending on the terminal). However, you could write it in a way that is likely to be understood by your users. If they are from the academic sector, you could use the latex-style way to express superscripts, e.g. x^2

Unicode character is the solution!


There is a very easy way to print superscripts and subscripts using Unicode characters. Do the following:

  • Press alt+f2
  • Type "charmap"

On doing so, you'll get tons of characters including subscripts, superscripts, etc. On the bottom left end of the window, you'd see something like 'U-xxxx' where x can be any alpha-numeric character(e.g 1,2,A,B..).

For example:

  • If you want to print a string x^2, you should write the string as:

    'x\u00b2', where u00b2 is 'U+00B2' shown in the Character Map.

This is how I used Character Map in my tkinter code snippet and it worked without any errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!