How to Set Column alignment in Python tabulate
The tabulate
module in Python allows you to format tabular data into beautifully structured tables.
Proper column alignment enhances readability.
This tutorial will guide you through various methods to control column alignment in tabulate
.
Basic Column Alignment
Left Alignment
To left-align all columns in your table, use the colalign
parameter in the tabulate
function and set each column’s alignment to 'left'
.
from tabulate import tabulate data = [ ['Ahmed', 24, 'Engineer'], ['Fatima', 27, 'Data Scientist'], ['Mona', 22, 'Artist'] ] headers = ['Name', 'Age', 'Profession'] # Setting all columns to left alignment print(tabulate(data, headers, colalign=("left", "left", "left")))
Output:
Name Age Profession ------ ----- -------------- Ahmed 24 Engineer Fatima 27 Data Scientist Mona 22 Artist
All columns are aligned to the left, which aligns the text at the start of each cell.
Right Alignment
To right-align all columns, set the colalign
parameter to 'right'
for each column.
print(tabulate(data, headers, colalign=("right", "right", "right")))
Output:
Name Age Profession ------ ----- -------------- Ahmed 24 Engineer Fatima 27 Data Scientist Mona 22 Artist
Here, all columns are right-aligned.
Center Alignment
To center-align all columns, set the colalign
parameter to 'center'
for each column.
print(tabulate(data, headers, colalign=("center", "center", "center")))
Output:
Name Age Profession ------ ----- -------------- Ahmed 24 Engineer Fatima 27 Data Scientist Mona 22 Artist
In this example, all columns are center-aligned.
Specify Alignment for Individual Columns
You can set different alignments for each column by specifying a tuple of alignment options in the colalign
parameter.
# Different alignment for each column print(tabulate(data, headers, colalign=("left", "right", "center")))
Output:
Name Age Profession ------ ----- -------------- Ahmed 24 Engineer Fatima 27 Data Scientist Mona 22 Artist
Here, the ‘Name’ column is left-aligned, ‘Age’ is right-aligned, and ‘Profession’ is center-aligned.
Alignment with Different Table Formats
Grid Format
To use the grid format, specify tablefmt='grid'
in the tabulate
function, which adds borders around your table.
print(tabulate(data, headers, tablefmt='grid', colalign=("left", "center", "right")))
Output:
+--------+-------+----------------+ | Name | Age | Profession | +========+=======+================+ | Ahmed | 24 | Engineer | +--------+-------+----------------+ | Fatima | 27 | Data Scientist | +--------+-------+----------------+ | Mona | 22 | Artist | +--------+-------+----------------+
In the grid format, the custom alignments are preserved, and the table is enclosed with borders.
Pipe Format
You can also use the pipe format by setting tablefmt='pipe'
, which is useful for Markdown tables.
print(tabulate(data, headers, tablefmt='pipe', colalign=("right", "left", "center")))
Output:
| Name | Age | Profession | |-------:|:------|:--------------:| | Ahmed | 24 | Engineer | | Fatima | 27 | Data Scientist | | Mona | 22 | Artist |
In the pipe format, the table uses pipes |
to separate columns.
Orgtbl Format
Use the ‘orgtbl’ format for tables compatible with Emacs Org mode.
print(tabulate(data, headers, tablefmt='orgtbl', colalign=("center", "right", "left")))
Output:
| Name | Age | Profession | |--------+-------+----------------| | Ahmed | 24 | Engineer | | Fatima | 27 | Data Scientist | | Mona | 22 | Artist |
The ‘orgtbl’ format creates tables suitable for Org mode, with the specified alignments applied to each column.
RST Format
To produce tables in reStructuredText format, use tablefmt='rst'
.
print(tabulate(data, headers, tablefmt='rst', colalign=("left", "center", "right")))
Output:
====== ===== ============== Name Age Profession ====== ===== ============== Ahmed 24 Engineer Fatima 27 Data Scientist Mona 22 Artist ====== ===== ==============
The ‘rst’ format outputs tables compatible with reStructuredText, and the alignments control how the content is aligned within each cell.
Dynamic Alignment Based on Data Type
You can let tabulate
automatically align data based on their types using stralign
and numalign
.
mixed_data = [ ['ID101', 'Ahmed', 123.456], ['ID102', 'Fatima', 7890], ['ID103', 'Mona', 45.67] ] headers_mixed = ['ID', 'Name', 'Score'] print(tabulate(mixed_data, headers_mixed, numalign="right", stralign="left"))
Output:
ID Name Score ----- ------ ------- ID101 Ahmed 123.456 ID102 Fatima 7890 ID103 Mona 45.67
In this table, numeric data is right-aligned (numbers and floats), and string data is left-aligned.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.