Python spacing and aligning strings Python spacing and aligning strings python python

Python spacing and aligning strings


You should be able to use the format method:

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

You will have to figure out 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.


Try %*s and %-*s and prefix each string with the column width:

>>> print "Location: %-*s  Revision: %s" % (20,"10-10-10-10","1")Location: 10-10-10-10           Revision: 1>>> print "District: %-*s  Date: %s" % (20,"Tower","May 16, 2012")District: Tower                 Date: May 16, 2012


You can use expandtabs to specify the tabstop, like this:

print(('Location: ' + '10-10-10-10' + '\t' + 'Revision: 1').expandtabs(30))print(('District: Tower' + '\t' + 'Date: May 16, 2012').expandtabs(30))

Output:

Location: 10-10-10-10         Revision: 1District: Tower               Date: May 16, 2012