Categories
Python Answers

How to create column output in Python?

Spread the love

Sometimes, we want to create column output in Python.

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

How to create column output in Python?

To create column output in Python, we can call format on a format string.

For instance, we write

table_data = [
    ['a', 'b', 'c'],
    ['aaaaaaaaaa', 'b', 'c'], 
    ['a', 'bbbbbbbbbb', 'c']
]

for row in table_data:
    print("{: >20} {: >20} {: >20}".format(*row))

to loop through table_data and call print in the loop with the format string to format the items in row into neatly formatted columns.

We use {: >20} to add placeholders for a column column space.

Conclusion

To create column output in Python, we can call format on a format string.

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 *