Print Output In Table Format In Python

Print Output In Table Format In Python

In this article, we will look at how to Print Output In Table Format In Python. We will see all the methods that are used for creating and deleting rows and columns with examples and will go deep into all the methods for better understanding.

There are 4 libraries that are used to Print Output In Table Format In Python:-

  1. Tabulate Library
  2. PrettyTable Library
  3. Texttable Library 
  4. Termtables Library

Tabulate Library in Python

We can use this library to print tabular data in python and to do so we use the tabulate class inside the tabulate library. It is a command line utility.

Installation

pip install tabulate

Usage

# Importing tabulate class from tabulate library
from tabulate import tabulate


# Creating a list that we want to tabulate
my_list = [
    ["Python", "Guido van Rossum", 1991],
    ["C", "Dennis Ritchie", 1972],
    ["Java", "James Gosling", 1995],
    ["JavaScript", "Brendan Eich", 1995],
    ["Php", "Rasmus Lerdorf", 1995],
    ["C++", "Bjarne Stroustrup", 1985],
    ["C#", " Anders Hejlsberg", 2000]
]

# Printing the tabulated list
print(tabulate(my_list))

Output:

Output for Tabulate Library in Python

Adding Headers to Table

# Printing the tabulated list
print(tabulate(my_list, headers=["Programming Languages", "Founder", "Release Year"]))

Output:

Output after Adding Headers to Table in Tabulate Library in Python
Output after Adding Headers to Table in Tabulate Library in Python

Show the Index of each row

# Printing the tabulated list
print(tabulate(my_list, headers=["S.on", "Programming Languages", "Founder", "Release Year"], showindex="always"))

Output:

Output for showing index in table in Tabulate Library in Python
Output for showing index in table in Tabulate Library in Python

Change Alignment of Values

# Printing the tabulated list
print(tabulate(my_list, headers=["Programming Languages", "Founder", "Release Year"], stralign="right", numalign="right"))

Output:

Output for alignment of values in Tabulate Library in Python

PrettyTable Library in Python

We have already discussed this library in detail, so click here if you want to know in detail about this library. Let’s have an example of PrettyTable library usage for printing output in Table format.

# Importing PrettyTable class from prettytable library
from prettytable import PrettyTable

# Creating instance of PrettyTable class
my_table = PrettyTable(["Programming Languages", "Founder", "Release Year"])

# Creating a list that we want to tabulate
my_table.add_row(["Python", "Guido van Rossum", 1991])
my_table.add_row(["C", "Dennis Ritchie", 1972])
my_table.add_row(["Java", "James Gosling", 1995])
my_table.add_row(["JavaScript", "Brendan Eich", 1995])
my_table.add_row(["Php", "Rasmus Lerdorf", 1995])
my_table.add_row(["C++", "Bjarne Stroustrup", 1985])
my_table.add_row(["C#", " Anders Hejlsberg", 2000])

# Printing the tabulated list
print(my_table)

Output:

Output for PrettyTable Library in Python

Texttable Library in Python

We use this library to print ASCII Table ( Tabular Form) in python and to do so we use the Texttable class from the Textable library.

Installation

pip install texttable

Usage

# Importing Texttable class from texttable Library
from texttable import Texttable

# Creating an instance of Texttable class
my_table = Texttable()

# Adding rows to our tabular table
my_table.add_rows(
    [["Programming Languages", "Founder", "Release Year"],
    ["Python", "Guido van Rossum", 1991],
    ["C", "Dennis Ritchie", 1972],
    ["Java", "James Gosling", 1995],
    ["JavaScript", "Brendan Eich", 1995],
    ["Php", "Rasmus Lerdorf", 1995],
    ["C++", "Bjarne Stroustrup", 1985],
    ["C#", " Anders Hejlsberg", 2000]]
)

# Printing Tabulated Data Using draw() function of Texttable Class
print(my_table.draw())

Output:

Texttable Library output in Python

Change Alignment of Values

To change the alignment of our table data in Texttable class, we have to use one String character array as an input parameter of set_cols_align() which is an in-built function of Texttable class. The one String character aligns our data for the corresponding column. Valid string characters for alignment:-

  • “l”– side alignment.
  • “r”– right side alignment.
  • “c”– center alignment.
  • “t”– top side alignment.
  • “m”– middle alignment.
  • “b”– bottom side alignment.

Add the below statement before adding rows to our tabular table in the code

my_table.set_cols_align(["l", "r", "l"])

Output:

Output for Change Alignment of Values in Texttable Library in Python

Termtables Library in Python

Installation

pip install termtables

Usage

#  Importing Termtables class
import termtables

# Adding data in my_table list
my_table = [
    ["Python", "Guido van Rossum", 1991],
    ["C", "Dennis Ritchie", 1972],
    ["Java", "James Gosling", 1995],
    ["JavaScript", "Brendan Eich", 1995],
    ["Php", "Rasmus Lerdorf", 1995],
    ["C++", "Bjarne Stroustrup", 1985],
    ["C#", " Anders Hejlsberg", 2000]
]

# Printing tubulated data using termtables
termtables.print(my_table)

Output:

Termtables Library output in Python
Termtables Library output in Python

Adding Headers to Table

# Creating a header list for column headers
headers = ["Programming Languages", "Founder", "Release Year"]

# Printing tubulated data using termtables
tt.print(my_table, header = headers)

Output:

Termtables Library output with headers
Termtables Library output with headers

Changing Alignment and adding Padding Tables

# Printing tubulated data using termtables
tt.print(my_table, header=headers, padding=(0, 1), alignment="lcr")

Output:

Termtables Library output for Changing Alignment and adding Padding Tables

We hope this article on Tabular Data in Python helps you.

Thank you for reading this article, click here to start learning Python in 2022.


Also Read:

Share:

Author: Ayush Purawr