How to Create Pretty Tables Using Python

how to make pretty tables using python
pip install prettytable

After the command above executes successfully create a file and name it main.py open it in your favorite text editor and insert the code below.

from prettytable import PrettyTable 

The above code first imports the PrettyTable class from prettytable module into our file. Next, let’s create a simple table with a few values.

x = PrettyTable()
x.field_names = ["Model","Year","Propulsion","Color","Shape"]
x.add_row(["Tesla","2013","Electric","Red","Saloon"])
x.add_row(["Ford f-150","2015","Electric","Blue","Truck"])
x.add_row(["Chrysler","2014","Petrol","Blue","Saloon"])
x.add_row(["Toyota Tacoma","2016","Petrol","Green","Pickup"])
x.add_row(["Subaru ","2019","Petrol","silver","Saloon"])
x.add_row(["Fiat","2017","Petrol","violet","hatchback"])
x.add_row(["Man","2020","Diesel","yellow","Tipper"])
x.add_row(["Caterpillar","2005","Diesel","Blue","Grader"])
print(x)

After creating the rows above let’s now save our file and run the command below.

python3 main.py

After running the command above you should see the table below.

Leave a Comment

Your email address will not be published. Required fields are marked *