How to Print Tables in Python?


In Python, there are various ways to print tables. This can be done by using the format() function or the tabulate() function. The texttable, beautifultable, PrettyTable modules can also be used to print tables in Python.

The format() function

The format() function can be used to print dictionaries and lists. The syntax for the format() function is given below.

print(“{} {}”.format(dict, list))
Code language: CSS (css)

Example:

print({‘Name’ : "John", ‘Age’ : 30}) #Output: {‘Name’: "John", ‘Age’: 30}
Code language: PHP (php)

The tabulate() function

The tabulate() function is used to print dict and lists. The syntax for the tabulate() function is given below.

tabulate(dict / list)
Code language: PHP (php)

Example:

from tabulate import tabulate dict = {‘Name’ : "John", ‘Age’ : 30} list = [{"Emp ID" : 101, "Name" : "John"}, {"Emp ID" : 102, "Name" : "Jennie"}] # printing dict as a table print(tabulate(dict)) #Output: # Age Name # 30 John # printing list of dicts as a table print(tabulate(list)) #Output: #Emp ID Name 101 John 102 Jennie
Code language: PHP (php)
tabulate

You also add a title for the table with param: headers=[“Emp ID”, “Name”]

print(tabulate(list, headers=["Emp ID", "Name"]), tablefmt='fancy_grid')
Code language: PHP (php)

The Prettytable module

The Prettytable library is a different way to solve the problem, with some unique features. We will use the PrettyTable() class in Python to define, change, and print tables.

Here’s how to define a table object with the header information and then use the add rows() method to add multiple rows at once:

I create a file named create_row.py.

from prettytable import PrettyTable x = PrettyTable() x.field_names = ["City name", "Area", "Population", "Annual Rainfall"] x.add_row(["1_Adelaide", 1293, 1158259, 600.5]) x.add_row(["2_Brisbane", 5903, 1857594, 1146.4]) x.add_row(["3_Darwin", 112, 120901, 1714.7]) x.add_row(["4_Hobart", 1357, 205556, 619.6]) x.add_row(["5_Sydney", 2058, 4336374, 1214.8]) x.add_row(["6_Melbourne", 1566, 3806092, 646.9]) x.add_row(["7_Perth", 5385, 1554768, 869.4]) print(x)
Code language: JavaScript (javascript)

Output

$ python create_row.py 
+-------------+------+------------+-----------------+
| City name   | Area | Population | Annual Rainfall |
+-------------+------+------------+-----------------+
|  1_Adelaide | 1293 |  1158259   |      600.5      |
|  2_Brisbane | 5903 |  1857594   |      1146.4     |
|   3_Darwin  | 112  |   120901   |      1714.7     |
|   4_Hobart  | 1357 |   205556   |      619.6      |
|   5_Sydney  | 2058 |  4336374   |      1214.8     |
| 6_Melbourne | 1566 |  3806092   |      646.9      |
|   7_Perth   | 5385 |  1554768   |      869.4      |
+-------------+------+------------+-----------------+

Read more about PrettyTable

In this blog post we discussed how to print tables using various methods in Python. We saw how the format() function can be used to print dict and lists. We also saw how the tabulate() function can be used to print dict and lists along with the PrettyTable module.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts