Python pretty print table | Example code

Use PrettyTable from prettytable module to pretty print table in Python. There are several ways to print tables in python, namely:

  • Using tabulate() function to print dict and lists
  • texttable
  • PrettyTable

Python pretty print table example

First, install a pretty table package then import module it into a project file.

from prettytable import PrettyTable

PTables = PrettyTable()

PTables = PrettyTable()
PTables.field_names = ["Selection No.", "Fruits", "Price"]
PTables.add_row(["0", "Apple", "1 $"])
PTables.add_row(["1", "Orange", "2.5 $"])
PTables.add_row(["2", "Grapes", "5 $"])
PTables.add_row(["3", "Cherry", "6 $"])


Python pretty print table

Read more PrettyTable:

Another examples

There are some light and useful python packages for this purpose:


from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))
Name      Age
------  -----
Alice      24
Bob        19


from texttable import Texttable
t = Texttable()
t.add_rows([['Name', 'Age'], ['Alice', 24], ['Bob', 19]])
| Name  | Age |
| Alice | 24  |
| Bob   | 19  |

Do comment if you have any doubts and suggestions on this Python print table topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.