Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print Superscript and Subscript in Python?
Python provides several methods to print superscript and subscript characters for mathematical expressions, chemical formulas, and formatted text output. This article explores different approaches using Unicode characters, custom functions, and external libraries.
Understanding Superscript and Subscript
Superscript: Text that appears above the baseline, commonly used for mathematical exponents like x² or footnotes. The smaller-sized text is raised above the normal text line.
Subscript: Text that appears below the baseline, typically used in chemical formulas like H?O or mathematical notations. The smaller-sized text is lowered below the normal text line.
Method 1: Using Unicode Characters
Python supports Unicode characters directly, allowing you to display superscript and subscript numbers using specific Unicode codes ?
Unicode Reference Tables
| Digit | Superscript Unicode | Subscript Unicode |
|---|---|---|
| 0 | U+2070 (?) | U+2080 (?) |
| 1 | U+00B9 (¹) | U+2081 (?) |
| 2 | U+00B2 (²) | U+2082 (?) |
| 3 | U+00B3 (³) | U+2083 (?) |
Example
# Superscript examples
print("Area = x\u00b2") # x²
print("E = mc\u00b2") # E = mc²
# Subscript examples
print("Water: H\u2082O") # H?O
print("Methane: CH\u2084") # CH?
Area = x² E = mc² Water: H?O Methane: CH?
Method 2: Creating a Custom Function
You can create a reusable function to convert regular numbers into superscript or subscript characters using the str.maketrans() and translate() methods ?
def convert_to_script(text, is_superscript=True):
normal = "0123456789"
superscript = "?¹²³??????"
subscript = "??????????"
if is_superscript:
mapping = str.maketrans(normal, superscript)
else:
mapping = str.maketrans(normal, subscript)
return text.translate(mapping)
# Test the function
equation = "X2 + Y3 = Z4"
print("Original:", equation)
print("Superscript:", convert_to_script(equation, True))
print("Subscript:", convert_to_script(equation, False))
Original: X2 + Y3 = Z4 Superscript: X² + Y³ = Z? Subscript: X? + Y? = Z?
Method 3: Using unicodeit Library
The unicodeit library converts LaTeX-style notation to Unicode characters. Use ^ for superscripts and _ for subscripts ?
Installation
pip install unicodeit
Example
import unicodeit
# Mathematical expressions
print(unicodeit.replace('E = mc^2'))
print(unicodeit.replace('x^2 + y^2 = r^2'))
# Chemical formulas
print(unicodeit.replace('H_2SO_4'))
print(unicodeit.replace('C_6H_12O_6'))
E = mc² x² + y² = r² H?SO? C?H??O?
Comparison of Methods
| Method | Pros | Cons | Best For |
|---|---|---|---|
| Unicode | No dependencies, fast | Manual coding required | Simple expressions |
| Custom Function | Reusable, flexible | Limited to digits | Repeated conversions |
| unicodeit | LaTeX syntax, comprehensive | External dependency | Complex expressions |
Common Applications
- Mathematical expressions: x², E=mc², quadratic formulas
- Chemical formulas: H?O, CO?, H?SO?
- Scientific notation: 1.23×10?
- Footnotes and references: Citation¹, trademark?
Conclusion
Python offers multiple approaches for printing superscript and subscript text. Use Unicode for simple cases, custom functions for reusability, or the unicodeit library for complex mathematical expressions. Choose the method that best fits your project's requirements and complexity.
