How to Make Subscript & Superscript Text in Python

How to Make Subscript & Superscript Text in Python

How to Make Subscript & Superscript Text in Python

When you start learning Python, strings feel simple: just text inside quotes. But then one day you want something more expressive:

H₂O instead of H2O

x² instead of x2

E = mc² instead of E = mc2

…and suddenly you realize Python has no built-in “superscript button” like MS Word.

⭐ FAQ: Subscript & Superscript in Python

  1. Does Python support superscript and subscript by default?
    No. Python does not have built-in superscript or subscript formatting. But it can display Unicode superscript/subscript characters, or you can use libraries like Rich, Matplotlib, or report generators.
  2. How do I print superscript like x² in Python?
    Use Unicode superscript characters:
    print("x²")
  3. How do I print subscript like H₂O?
    Use Unicode subscript:
    print("H₂O")
  4. How do I convert normal numbers into superscript automatically?
    Use a translation map:
    "123".translate(str.maketrans("123", "¹²³"))
  5. Can I use superscript/subscript in Tkinter?
    Yes. Tkinter labels support Unicode:
    label = tk.Label(text="πr²")
  6. How do I put superscripts/subscripts in Matplotlib?
    Use LaTeX-style formatting:
    plt.title("x$^2$ + y$_3$")
  7. Does Python support HTML-like <sup> <sub> tags?
    Not in the terminal or basic print. But they work in:
    • web frameworks (Flask, Django)
    • PDF generators
    • Markdown/HTML rendering libraries
  8. Why does my terminal not show superscript/subscript?
    Some terminals/fonts don’t support full Unicode. Try changing the font to:
    • DejaVu Sans Mono
    • Consolas
    • Fira Code
  9. Can I convert whole formulas automatically?
    Yes. This small function converts "^" and "_" notation:
    def convert(text): ...
    I can provide the full version if you want.
  10. Can I use superscripts in Python variable names?
    No. Variable names must use standard ASCII letters/digits/underscore only.
  11. Can superscript/subscript be used in PDF reports?
    Yes, using:
    • ReportLab
    • LaTeX
    • HTML → PDF converters
  12. Which method is best for beginners?
    Unicode is the simplest and works everywhere:
    print("X² + Y² = H₂O")

But here’s the fun part: Python can do it — you just need to know which technique fits which job.

This article will take you through the real, practical ways developers use subscripts and superscripts in Python. Not shortcuts, but clear explanations that make you say, “Finally, I understand!”

✨ 1. The Secret Superpower: Unicode Characters

Before Python came, before laptops came — the Unicode standard existed to support multilingual characters. Among thousands of characters, Unicode also stores tiny numbers, like ₀₁₂ and ⁰¹².

Python prints them naturally.

🔹 Example

print("H₂O")
print("x² + y³")

You don’t need a library. You don’t need a setup. It just works.

Why this matters

If you’re displaying text in:

  • console
  • Tkinter
  • a terminal app
  • logs

…Unicode is your simplest friend.

✨ 2. Auto-convert Digits Into Subscript/Superscript

Typing “₃” or “⁵” every time gets boring.

Let Python do the work.

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

def to_subscript(text):
    return text.translate(SUB)

def to_superscript(text):
    return text.translate(SUP)

print("H" + to_subscript("2") + "O")
print("x" + to_superscript("2"))

Now you can feed entire strings and convert them like magic.

✨ 3. When You Need Beautiful Output: Rich Library

Sometimes, the terminal just isn’t enough. You want something that looks styled, like a modern UI.

The Rich library lets you do that.

pip install rich
from rich import print

print("H₂O")
print("[sup]x2[/sup] and [sub]A3[/sub]")

This is great for:

  • CLI apps
  • teaching programs
  • styling terminal dashboards

✨ 4. When You’re Building a GUI: Tkinter & Unicode

GUI toolkits usually support Unicode characters directly, so simply placing “²” or “₃” works beautifully.

import tkinter as tk

root = tk.Tk()
root.title("Subscript & Superscript Demo")

label = tk.Label(
    root, 
    text="H₂O  |  x² + y³  |  E = mc²",
    font=("Arial", 24)
)
label.pack(pady=20)

root.mainloop()

If you’re building:

  • a calculator
  • chemistry teaching tool
  • math app
  • science quiz

…this is perfect.

OUTPUT:

Building a GUI: Tkinter & Unicode

✨ 5. When You’re Plotting Graphs: Use Math Notation in Matplotlib

import matplotlib.pyplot as plt

plt.title("Graph of x$^2$ + y$_3$")
plt.plot([1,2,3], [1,4,9])
plt.show()

Great for:

  • charts
  • scientific reports
  • mathematical visualisations

When Matplotlib encounters LaTeX math syntax such as x^2 or y_3, it renders them as x² and y₃, using superscript and subscript formatting.

This looks sharp and professional.

OUTPUT:

Plotting Graphs: Use Math Notation in Matplotlib

✨ 6. When You're Generating PDFs or Reports

Libraries like:

  • ReportLab
  • Markdown → PDF
  • LaTeX generators

allow superscripts/subscripts using:

  • HTML (<sup>2</sup>)
  • Markdown (x^2)
  • LaTeX (x^{2})

🌿 Which Method Should You Use?

SituationBest Method
Terminal printingUnicode
Teaching/CLI dashboardRich
GUI apps (Tkinter, PyQt, Kivy)Unicode
Charts/graphsMatplotlib formulas
PDF/text generationHTML/LaTeX/Markdown formats

🌟 Final Thoughts — Why This Topic Matters

Superscripts and subscripts look small, but they carry big meaning. A tiny “²” can change the entire meaning of an equation. A small “₃” can turn normal text into real chemistry notation.

Python doesn’t force you into one method — it gives you choices, each perfect for a different purpose.

If you’re writing apps, teaching tools, scientific programs, or just decorating your text… subscripts and superscripts help you speak a richer language.

And now you know every clean and modern way to use them in Python.

📘 Practice Yourself

  1. Create a Python program that converts any number into superscript using a function.
  2. Write a script that prints a chemical formula like CO₂, SO₄²⁻ using Unicode only.
  3. Build a small Tkinter window that displays:
    • x² + y³
    • H₂O
    • E = mc²
  4. Plot a graph in Matplotlib with:
    • Title using superscript
    • X-axis label using subscript
    Example: y = x², label x₁
  5. Make a Python function that takes text like "H_2O + x^3" and converts it into Unicode subscripts/superscripts.
  6. Using Rich library, style a terminal output that shows:
    • superscript values
    • subscript values
    • colored math expressions
  7. Challenge :
Change the displayed text to show any three scientific formulas using subscripts/superscripts, e.g.:

CO₂
a² + b² = c²
Na⁺, Cl⁻

✔ Use at least two different colors.
✔ Use a custom font of your choice.
Challenge 1 — Make Your Own Colorful Formula Display
Previous Post Next Post

Contact Form