Python Tutorial 25: How to do formatting print statements in Python

From my previous post – Python Tutorial 21 : How to read/write a CSV file in Python? – when we read the CSV file, in the end, we have created a very terrible result as follow. Now we are going to rewrite the program and format the print statement.

How to read a CSV file in Python - Result

The following screen captured all lines we have to change

Formatting print statement (String) - Python

We have removed the nested for loop as it is no longer suitable and replace it with following statement

[vtftable cols=”{0}0:fff2cc;{/}”]

In the slot 0 – {0:<26} – we are allowing 26 spaces and follow by slot 1. Then we use the “.format” function to define the item for each slots.

Following is the output:Formatting print statement (String) Result - Python

The left arrow “<” indicated the alignment. We can have following options:

< – Left alignment

^ – center alignment

> – RIght alignment

nothing – simply reserve spaces.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.