Finally, a Way to Print Tables in Python That Don't Look Terrible
You've written a script that produces a nice, clean dataset. You print it to the terminal, and it comes out as a jumbled wall of values that's impossible to read. If you've ever found yourself manually padding strings with spaces just to line up columns, you know the pain. PrettyTable is a Python library that handles that formatting for you, producing clean ASCII tables with a single import.
What It Does
PrettyTable is a Python package that renders tabular data as formatted ASCII tables. You create a table, tell it what your columns are, feed it your data, and it handles the alignment, borders, and spacing. The output looks like this:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
It's a pure Python library, installable via pip, and it works with any Python 3 environment. The API gives you several ways to populate a table, so you can pick whichever approach fits your data source.
Why It's Cool
The library's real strength is how flexible it is about getting data in. Most table libraries force you into one pattern—usually "build a list of rows first." PrettyTable doesn't care how your data is shaped.
-
Three ways to load data. You can add rows one at a time with
add_row, load a whole list of rows at once withadd_rows, or build the table column by column withadd_column. That last one is genuinely useful when your data comes from separate lists (say, parallel arrays from an API response) rather than a list of tuples. -
Field names are just an attribute. You set
table.field_namesto a list of strings and you're done. No constructor ceremony, no builder pattern. It's straightforward and readable. -
It's a small, focused tool. This isn't trying to be a data frame library or a charting package. It prints tables. That's it. The scope is narrow, which means the API stays simple and the dependency footprint stays small.
-
You can see it before you use it. The README points to a demo you can run with
python3 -m prettytable. That's a small touch, but it's nice when a library lets you see the output format before you commit to installing it. -
It's actively maintained. The badges tell the story: PyPI releases, a supported Python version matrix, GitHub Actions CI, Codecov coverage tracking, and Black for code style. This isn't a abandoned weekend project—it gets attention.
The honest pitch here is that PrettyTable solves a boring problem well. Formatting text tables is the kind of thing you don't want to think about, and this library means you don't have to.
How to Try It
Installation is a single command. If you're on Python 3:
python3 -m pip install -U prettytable
If you want the latest development version straight from the repository:
python3 -m pip install -U git+https://github.com/prettytable/prettytable
Or if you manage dependencies through a requirements.txt file:
-e git://github.com/prettytable/prettytable.git#egg=prettytable
Once it's installed, you can run the built-in demo to see what the output looks like:
python3 -m prettytable
And here's the basic usage. Start by creating a table and setting your field names:
from prettytable import PrettyTable
table = PrettyTable()
table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
Then add your data. Row by row:
table.add_row(["Adelaide", 1295, 1158259, 600.5])
table.add_row(["Brisbane", 5905, 1857594, 1146.4])
Or all at once:
table.add_rows([
["Adelaide", 1295, 1158259, 600.5],
["Brisbane", 5905, 1857594, 1146.4],
["Darwin", 112, 120900, 1714.7],
])
Or column by column:
table.add_column("City name", ["Adelaide", "Brisbane", "Darwin"])
table.add_column("Area", [1295, 5905, 112])
When you print the table, PrettyTable renders the full ASCII output. You can find the repository and the full documentation at github.com/prettytable/prettytable.
Final Thoughts
PrettyTable is the kind of library that earns its place in your toolkit by being unremarkable in the best way. It does one thing, it does it correctly, and it doesn't ask you to learn a new paradigm to use it. If you write command-line tools, generate reports, or just want your debug output to be legible, this is worth having on hand. It's not going to change how you think about Python, but it will save you from writing another broken column-alignment function—and honestly, that's a win.