HOWTO · Python
How to Print Data in Tabular Format in Python
This tutorial demonstrates how to print data in a tabular format in Python
On this page
Lists preserve the order of their elements, but printing a list of rows directly can make the relationship between values hard to scan. Formatting each row as columns makes a small report easier to read in a terminal. This guide uses the same three student records for each approach, so you can compare the methods and choose one that fits your program.
Format List Rows with str.format()
The string method str.format() gives you direct control over each column without installing a package. In a format field such as "{:<8}", < aligns a value to the left and 8 sets a minimum field width of eight characters. The width adds padding when a value is shorter; it does not truncate a longer value, so long entries can push later columns out of alignment. Increase the width when your data needs more room.
Run this example:
rows = [["Mark", 12, 95], ["Jay", 11, 88], ["Jack", 14, 90]]
columns = ["Name", "Age", "Percent"]
row_format = "{:<8} {:<8} {:<10}"
print(row_format.format(*columns))
for row in rows:
print(row_format.format(*row))
This prints a header followed by one line per row. Adjust the widths in row_format to suit the longest values you expect. This approach works well for a small, fixed set of columns when you want to keep the output layout explicit.
Print Rows with tabulate
The optional tabulate package builds a readable text table from row data and headers. Install it in the Python environment used by your program with python -m pip install tabulate. Then pass the rows and column labels to tabulate():
import importlib.util
if importlib.util.find_spec("tabulate") is None:
print("Install tabulate with: python -m pip install tabulate")
else:
from tabulate import tabulate
rows = [["Mark", 12, 95], ["Jay", 11, 88], ["Jack", 14, 90]]
headers = ["Name", "Age", "Percent"]
print(tabulate(rows, headers=headers))
When installed, the package chooses widths based on the values and adds a separator beneath the header. This avoids maintaining padding strings yourself, while the selected table style remains the package’s responsibility. Other packages, including PrettyTable, termtable, and texttable, offer their own formatting options.
Display List Data with pandas.DataFrame
Use pandas.DataFrame when the same records are also part of a data-analysis workflow. Install pandas in the environment that runs the script with python -m pip install pandas. Supply the list of rows and column names to the constructor, then print the resulting DataFrame:
import importlib.util
if importlib.util.find_spec("pandas") is None:
print("Install pandas with: python -m pip install pandas")
else:
import pandas as pd
rows = [["Mark", 12, 95], ["Jay", 11, 88], ["Jack", 14, 90]]
columns = ["Name", "Age", "Percent"]
table = pd.DataFrame(rows, columns=columns)
print(table)
Pandas displays a row index along with the named columns. A DataFrame is useful when you will continue selecting, transforming, or summarizing the records; for a one-off terminal table, it adds a dependency that a formatting string or tabulate may avoid.
Choose a Table-Printing Approach
Choose str.format() for a dependency-free report whose column widths you want to set explicitly. Choose tabulate when you want a convenient terminal table and can add that package to the project environment. Choose pandas.DataFrame when printing is one step in a broader data task and the project already uses pandas.
All three examples start with the same list-of-lists structure: each inner list is a row, and values at the same position belong to the same column. Keep the column labels in the same order as those positions. If records have inconsistent row lengths or values wider than your chosen fields, normalize the data or adjust the formatting before relying on fixed-width alignment.