Python spacing and aligning strings

Tristan Forward picture Tristan Forward · May 16, 2012 · Viewed 119.8k times · Source

I am trying to add spacing to align text in between two strings vars without using " " to do so

Trying to get the text to look like this, with the second column being aligned.

Location: 10-10-10-10       Revision: 1
District: Tower             Date: May 16, 2012
User: LOD                   Time: 10:15

Currently have it coded like this, just using spaces...

"Location: " + Location + "               Revision: " + Revision + '\n'

I tried working with string.rjust & srting.ljust but to no avail.

Suggestions?

Answer

IronMensan picture IronMensan · May 16, 2012

You should be able to use the format method:

"Location: {0:20} Revision {1}".format(Location,Revision)

You will have to figure out the of the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.