How to make a Table in Python?
Last Updated :
24 Feb, 2025
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.
Python
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.
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.
Python
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:
OutputExplanation: 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.
Python
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().
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.
Python
# 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:
OutputExplanation: 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.
Similar Reads
How to Make Arrays fit into Table in Python Pandas? To convert arrays into a table (DataFrame) in Python using the Pandas library, you can follow the steps depending on the structure of your array:1. One-Dimensional ArrayTo convert a one-dimensional NumPy array into a DataFrame, use the pd.DataFrame() method and specify column names for better readab
2 min read
How to Create Frequency Tables in Python? In this article, we are going to see how to Create Frequency Tables in Python Frequency is a count of the number of occurrences a particular value occurs or appears in our data. A frequency table displays a set of values along with the frequency with which they appear. They allow us to better unders
3 min read
How to create Tables using Plotly in Python? Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
How to Add a Column to a MySQL Table in Python? Prerequisite: Python: MySQL Create Table Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. Â ALTER statemen
4 min read
Convert CSV to HTML Table in Python CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma.Example :Â Â Name, Salary, Age, No.of year
2 min read
TextTable module in Python It is a python module, which helps us to print table on terminal. It is one of the basic python modules for reading and writing text tables in ASCII code. It aims to make the interface as similar as possible like csv module in Python. The texttable module supports both fixed-size tables (where colum
3 min read
Matplotlib.axes.Axes.table() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
3 min read
How to Print Out All Rows of a MySQL Table in Python? MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Â In order to access MySQL databases from a
2 min read
Introduction to Python Tabulate Library The Python tabulate module is a library and a command-line utility that displays data in a visually appealing format (tabulate format). Whether working with lists, dictionaries, pandas DataFrames, or other forms of structured data, the tabulate module can convert raw data into well-formatted tables.
6 min read
How to Count the Number of Rows in a MySQL Table in Python? MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a
2 min read