27

In Python 3.3, is there any way to make a part of text in a string subscript when printed?

e.g. H₂ (H and then a subscript 2)

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
samrobbins
  • 451
  • 2
  • 6
  • 11

5 Answers5

46

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.

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • 1
    +1. @Sam: To emphasize, the only subscript that can directly be done like this requires the existence of the special characters (in Unicode). This way, the 2 and 4 in `'H₂SO₄'` are actually different characters than 2 and 4. Yet, it is rather unusual way to implement the subscript and superscript. As Bakuriu mentioned in http://stackoverflow.com/a/24391972/1346705, the usual way is to use something more than a viewer capable to display Unicode characters. – pepr Jun 24 '14 at 21:00
  • This code is great in Python 3 but breaks in Python 2: [Python 2 maketrans() function doesn't work with Unicode: “the arguments are different lengths” when they actually are](https://stackoverflow.com/questions/30108869/python-maketrans-function-doesnt-work-because-the-arguments-are-different-len) – smci Sep 10 '18 at 05:57
33

The output performed on the console is simple text. If the terminal supports unicode (most do nowadays) you can use unicode's subscripts. (e.g H₂) Namely the subscripts are in the ranges:

  • 0x208N for numbers, +, -, =, (, ) (N goes from 0 to F)
  • 0x209N for letters

For example:

In [6]: print(u'H\u2082O\u2082')
H₂O₂

For more complex output you must use a markup language (e.g. HTML) or a typesetting language (e.g. LaTeX).

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
19

Using code like this works too:

print('\N{GREEK SMALL LETTER PI}r\N{SUPERSCRIPT TWO}')
print('\N{GREEK CAPITAL LETTER THETA}r\N{SUBSCRIPT TWO}')

The output being:

πr²
Θ₂

Note that this works on Python versions 3.3 and higher only. Unicode formatting.

Shirsho
  • 313
  • 2
  • 6
15

If you want to use it on the axes of a plot you can do:

import matplotlib.pyplot as plt
plt.plot([1])
plt.ylabel(r'$H_{2}$')
plt.show()

which gives

enter image description here

Details on the Tex markup language and how to use it in matplotlib can be found here.

user_na
  • 2,154
  • 1
  • 16
  • 36
R. Cox
  • 819
  • 8
  • 25
2

By using this code you can use alphabets on the superscript and subscript In This code format() is Function and in Format function ('\unicode')

By using this table (Unicode subscripts and superscripts on Wikipedia) you can give suitable unicode to the suitable one

you can use superscript and sub script

"10{}".format('\u00B2')  # superscript 2
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32