In this post we will be creating a Python script that will print coloured tables to output. To do this we will be using the ‘rich’ library which allows us to create aesthetically pleasing command line applications. The library is very useful whereby it allows formatting to many elements of output.
To install the rich library run the following command.
pip install rich
See the snippet of Python code below where we print a coloured table consisting of two columns name and age, in this example we have also assigned a table title. Note that the number of columns can be changed and you may add more if necessary, for a list of the standard colours available click here.
We start by defining our table and its title and then its column headings. We then move on to adding rows to our table and print this to output.
from rich.console import Console
from rich.table import Table
table = Table(title="Best Football Players")
table.add_column("Name", style="cyan")
table.add_column("Age", style="bright_green")
table.add_row("Lionel Messi", "35")
table.add_row("Cristiano Ronaldo", "37")
table.add_row("Raheem Sterling", "28")
table.add_row("Kylian Mbappé", "24")
table.add_row("Mohamed Salah", "30")
console = Console()
console.print(table)
An image of what this would look like whilst running can be seen below.

Take a look at some of our other content around the Python programming language by clicking here.