Categories
Python Answers

How to create nice column output in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *