How to make a Table in Python?
Creating a table in Python involves structuring data into rows and columns for clear representation. Tables can be displayed in various formats, including plain text, grids or structured layouts. Python provides multiple ways to generate tables, depending on the complexity and data size.
Using Tabulate
Tabulate module is the most efficient way to create tables. It offers various formatting styles, requires minimal code and automatically aligns data properly. This method is ideal for small to medium datasets where quick visualization is needed.
from tabulate import tabulate
# assign data
a = [
["Nikhil", "Delhi"],
["Ravi", "Kanpur"],
["Manish", "Ahmedabad"],
["Prince", "Bangalore"]
]
# create header
headers = ["Name", "City"]
print(tabulate(a, headers=headers, tablefmt="grid"))
Output:

Explanation: tabulate() function is called with three arguments: the data list, headers and tablefmt="grid", which specifies the grid-style formatting for better readability.
Table of Content
Using pandas.DataFrame
Pandas library is a powerful tool for handling large datasets. It provides easy-to-use table structures with built-in functions for filtering, sorting and exporting data. While it adds some overhead, it is the best choice for working with structured data at scale.
import pandas as pd
# assign data
a = {
"Name": ["Nikhil", "Ravi", "Manish", "Prince"],
"City": ["Delhi", "Kanpur", "Ahmedabad", "Bangalore"]
}
# create DataFrame
df = pd.DataFrame(a)
print(df)
Output:

Explanation: dictionary a is converted into a DataFrame using pd.DataFrame(a), which structures the data in a tabular format.
Using PrettyTable
PrettyTable offers an easy way to generate well-formatted tables with a clean, readable structure. It allows for customization, such as column alignment and border styles, making it useful for reports and console applications.
from prettytable import PrettyTable
# specify the Column Names while initializing the Table
table = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
# add rows
table.add_row(["Leanord", "X", "B", "91.2 %"])
table.add_row(["Penny", "X", "C", "63.5 %"])
table.add_row(["Howard", "X", "A", "90.23 %"])
table.add_row(["Bernadette", "X", "D", "92.7 %"])
table.add_row(["Sheldon", "X", "A", "98.2 %"])
table.add_row(["Raj", "X", "B", "88.1 %"])
table.add_row(["Amy", "X", "B", "95.0 %"])
print(table)
Output:

Explanation: This code initializes a table with column names ("Student Name", "Class", "Section", and "Percentage"). Rows containing student data are added using table.add_row().
Using string formatting
String formatting manually structures table data without any dependencies. While it gives full control over spacing and alignment, it lacks flexibility and automation, making it inefficient for large or dynamic datasets.
# assign data
a = [
["Nikhil", "Delhi"],
["Ravi", "Kanpur"],
["Manish", "Ahmedabad"],
["Prince", "Bangalore"]
]
# create header
header = ["Name", "City"]
# print header
print(f"{header[0]:<10} {header[1]:<15}")
print("-" * 25)
# print rows
for row in a:
print(f"{row[0]:<10} {row[1]:<15}")
Output:

Explanation: header is printed with fixed-width alignment using :< for proper spacing. A separator line ("-" * 25) enhances readability. A for loop iterates through a, printing each row with aligned columns to maintain a structured table format.