How to print Superscript and Subscript in Python?


Python is a multipurpose programming language which is well known to us for its simplicity and power. Whether you are starting as a fresher or an experienced developer, Python helps us with a wide range of tools to control and display text in various formats. Many times we need superscripts and subscripts as part of our writing especially to denote Scientific Notations or values. In this article, we will try to visualize and understand different ways to print superscripts and subscripts in Python. This will allow you to beautify and justify text output and make it visually appealing and equally correct.

Decoding Superscript and Subscript

Though most of you already are aware of Superscripts and Subscripts, still for a clarity of beginners or new learners let’s quickly understand what these two terms mean.

Superscript: When writing we follow a base line(imaginary) to maintain a flow and legibility. For certain kinds of text we need a formatting where a smaller-sized text is raised above the baseline, so as to be used for mathematical exponents, footnotes, or certain chemical formulas. That formatting technique is called Superscript. For example, in the expression "x2", the number 2 is in superscript

Subscript: Subscript is totally the opposite of superscript. This formatting technique lowers a smaller-sized text below the baseline, mostly used for chemical formulas, mathematical equations, or certain mathematical symbols. For example, in the chemical formula "H2O", the number 2 is in Subscript.

Now that we have a clear picture of superscript and subscript, let's understand how to print them in Python.

Method 1: Using Unicode Characters

Unicode is a standardized system that is accepted worldwide for character encoding of text expressed in most of the writing systems available. It assigns a unique number to every character across various writing systems. Python is one of the languages that supports Unicode, making it possible for us to display a vast range of characters, including superscript and subscript.

Let’s look into a few Unicode codes associated with the respective characters.

Superscript No.s

Unicode Code

Superscript Characters

0

U+2070

0

1

U+00B9

1

2

U+00B2

2

3

U+00B3

3

4

U+2074

4

5

U+2075

5

6

U+2076

6

7

U+2077

7

8

U+2078

8

9

U+2079

9

Subscript No.s

Unicode Code

Subscript Characters

0

U+2070

0

1

U+00B9

1

2

U+00B2

2

3

U+00B3

3

4

U+2074

4

5

U+2075

5

6

U+2076

6

7

U+2077

7

8

U+2078

8

9

U+2079

9

Python print() statement can understand Unicode if we add /u, which returns the character corresponding to a given Unicode code. Let’s code a simple program to test it out.

Example

# superscript
print("x\u00b2") # x²
# subscript
print(u'H\u2082O') # H2O

Output

x²
H2O

Method 2: Creating Custom Function

We can also create a function to improve flexibility and also use it accordingly to convert our text into superscript or subscript.

In our function, we can create two strings, one for normal characters and another with superscript/subscript characters.

The maketrans() method can be used then to create a link or mapping between the characters. Now we can simply pass the mapping to translate() method to replace the normal characters with the superscript/subscript characters.

With this function, you can easily convert and print any regular number as a superscript or subscript. Let’s look at our code to understand it better.

Example

def convert_text(text, is_superscript=True):
    normal_chars = "0123456789"
    superscript_chars = "⁰¹²³⁴⁵⁶⁷⁸⁹"
    subscript_chars = "₀₁₂₃₄₅₆₇₈₉"

    if is_superscript:
        mapping = str.maketrans(normal_chars, superscript_chars)
    else:
        mapping = str.maketrans(normal_chars, subscript_chars)
    converted_text = text.translate(mapping)
    return converted_text

input_text = 'X2'
superscript_text = convert_text(input_text, is_superscript=True)
subscript_text = convert_text(input_text, is_superscript=False)
print("Input Text: ", input_text)
print("Superscript Text:", superscript_text)
print("Subscript Text:", subscript_text)

Output

Input text: X2
Superscript text: X²
Subscript text: X₂

Method 3: Using univoceit Library

Other than the unicode method and custom function, we can also find our way through the vast library support of Python.

One such library is the UnicodeIt library, which converts LaTeX tags to unicode. We can use it’s replace() method to generate superscripts and subscripts.

For superscripts we use the tag ‘ ^ ’ and for subscripts we use tag ‘ _ ‘ inside replace() method. Before we can use it in our code we can easily install it by running the following command in cmd window:

pip install unicodeit

Now, once we have completed the installation of the package, we can import and use the library. Let’s dive into the code.

Example

import unicodeit
print(unicodeit.replace('X^2 + Y^2 = Z^2'))
print(unicodeit.replace('H_2SO_4'))

Output

X² + Y² = Z²
H₂SO₄

Application of superscript and subscript

  • Mathematical Equations:

    • Expressing exponents: "x^2" represents "x squared."

    • Scientific notation: "1.23 x 10^5" represents "1.23 times 10 to the power of 5."

  • Chemical Formulas:

    • Molecular formula: "H2O" represents water, where the "2" is a subscript indicating two hydrogen atoms.

    • Chemical equation: "2H2 + O2 → 2H2O" represents the formation of water from hydrogen and oxygen.

  • Footnotes and References:

    • Indicating footnotes: "This is a sentence with a superscript^1 that refers to a footnote."

    • Citations: "Einstein's theory of relativity^2 revolutionized physics."

  • Formatting Characters:

    • Trademarks: "OpenAITM" with the "TM" superscript.

    • Registered symbols: "OpenAI®" with the "R" superscript.

Conclusion

Superscripts and Subscripts are widely used for mathematical equations and chemical formulas. Nowadays, they are used to beautify or make the text visually attractive as well. With the help of Python and its powerful libraries like Unicodeit, we can have one line codes to implement these two writing formats easily to our scripts.

In this article we also came across how we can create our own function to convert normal text to superscript and subscripts. There are a lot of possibilities to customize and do try executing your own vision. We have seen coding examples to execute the possible commands.

The initial code uses Unicode support of Python to translate normal text to Superscripts and Subscripts. For the second example we have created a function that increases our flexibility and also promotes its use for multiple scenarios.

Lastly, we saw the power of libraries. Unicodeit helped us convert texts to superscripts and subscripts by using simple replace() method.

Updated on: 29-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements