How to Print Superscript in Python

How do you print superscript in Python?

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

Superscript for a variable in python

The escape code \u00b2 is a single character; you want something like

>>> print(eval(r'"\u00b' + str(2) + '"'))
²

Less convolutedly,

>>> print(chr(0x00b2))
²

This only works for superscript 2 and 3, though; the other Unicode superscripts are in a different code block.

>>> for i in range(10):
... if i == 1:
... print(chr(0x00b9))
... elif 2 <= i <= 3:
... print(chr(0x00b0 + i))
... else:
... print(chr(0x2070 + i))

¹
²
³






Printing subscript in python

If all you care about are digits, you can use the str.maketrans() and str.translate() methods:

example_string = "A0B1C2D3E4F5G6H7I8J9"

SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")

print(example_string.translate(SUP))
print(example_string.translate(SUB))

Which will output:

A⁰B¹C²D³E⁴F⁵G⁶H⁷I⁸J⁹
A₀B₁C₂D₃E₄F₅G₆H₇I₈J₉

Note that this won't work in Python 2 - see Python 2 maketrans() function doesn't work with Unicode for an explanation of why that's the case, and how to work around it.

Is it possible to print characters on top of each other without erasing the previous one in order to have both superscript and subscript?

In Jupyter Notebook/Lab this should work:

from IPython.display import Math

Math(r"1.23^{+4.56}_{-7.89}")

For convenience, you can package it in a class:

from IPython.display import Math

class PPrint:
def __init__(self, base, sub, sup):
self.base = base
self.sub = sub
self.sup = sup


def _ipython_display_(self):
display(Math(f"{{{self.base}}}^{{{self.sub}}}_{{{self.sup}}}"))

Then you can create an instance e.g.:

x = PPrint("1.23", "+4.56", "-7.89")

and if you execute in a notebook either x or display(x), it should appear as in your example.

Printing the exponent in scientific numbers with superscript characters

The issue with the superscript numbers is that the UTF-8 codes are not contiguous, you need a translation table.

Here is one way to do it with the help of f-strings:

def scientific_superscript(num, digits=2):
base, exponent = f'{num:.{digits}e}'.split('e')
d = dict(zip('-+0123456789','⁻⁺⁰¹²³⁴⁵⁶⁷⁸⁹'))
return f'{base}⋅10{"".join(d.get(x, x) for x in exponent)}'

scientific_superscript(1.1884423896339773e-06)
# '1.19⋅10⁻⁰⁶'

scientific_superscript(3.141e123)
# '3.14⋅10⁺¹²³'
blacklisting characters:

example for 0 and +

def scientific_superscript(num, digits=2):
base, exponent = f'{num:.{digits}e}'.split('e')
d = dict(zip('-+0123456789','⁻⁺⁰¹²³⁴⁵⁶⁷⁸⁹'))
return f'{base}⋅10{"".join(d.get(x, x) for x in exponent.lstrip("0+"))}'

scientific_superscript(3.141e3)
# '3.14⋅10³'

scientific_superscript(1e103)
# '1.00⋅10¹⁰³'

I need to superscript letters in python

Consoles don't support superscripts natively.

You can leave it like that, or use some special characters to show that (ˢᵗ), but those might not be supported in some fonts or terminals.



Related Topics



Leave a reply



Submit