How to create nice column output in Python?

Sometimes, we want to create nice column output in Python.

In this article, we’ll look at how to create nice column output in Python.

How to create nice column output in Python?

To create nice column output in Python, we can use format strings with print.

For instance, we write:

table_data = [['a', 'b', 'c'], ['aa', 'b', 'c'], ['a', 'bb', 'c']]
for row in table_data:
    print("{: >20} {: >20} {: >20}".format(*row))

We use the {: >20} to set each column to have at least 20 characters and align the text to the right.

Therefore, we get:

                   a                    b                    c
                  aa                    b                    c
                   a                   bb                    c

from the print output.

Conclusion

To create nice column output in Python, we can use format strings with print.