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)
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)
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:
+
,-
,=
,(
,)
(N
goes from0
toF
)For example:
For more complex output you must use a markup language (e.g. HTML) or a typesetting language (e.g. LaTeX).
If all you care about are digits, you can use the
str.maketrans()
andstr.translate()
methods: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.