I'm using the advanced Python expression to build a label and am having trouble with the padding function in the format function as follows (rows is an arcpy SearchCursor output):

for row in rows:  
     if row.DETECTFLAG=='N':
        texttoadd = ""
        width1= 60-len(row.CHEMICALNM)
        texttoadd = texttoadd + "{0}".format(row.CHEMICALNM) +"<BOL><ITA>{0:>{2}} {1}</ITA></BOL>".format(row.RRESTXT,row.LABQUALIFI,width1) + '\n'
        myText3 = myText3 + texttoadd
     elif row.FLAG == 'x' and row.DETECTFLAG=='Y':    
        texttoadd = ""        
        width2= 60-len(row.CHEMICALNM)
        texttoadd = texttoadd + "{0}".format(row.CHEMICALNM)+ "<BOL><ITA><CLR red='255'>{0:>{2}} {1}</CLR></ITA></BOL>".format(row.RRESTXT,row.LABQUALIFI,width2) + '\n'
        myText3 = myText3 + texttoadd    
        texttoadd = ""
        width3= 60-len(row.CHEMICALNM)
        texttoadd = texttoadd + "{0}".format(row.CHEMICALNM)+"<BOL><ITA>{0:>{2}} {1}</ITA></BOL>".format(row.RRESTXT,row.LABQUALIFI,width3)  + '\n'
        myText3 = myText3 + texttoadd

the result of the myText3 variable is this:

enter image description here

I'm trying to use the format script to line up the column on the right side of the box.

Benzene              123245 J
Benzo(A)Pyrene       123 J
Silver               123456789
1,2-Dichlorobenzene  ND U

I have tried to set a variable to the right justify (>{2} instead of an actual number) but it doesn't seem to work. My hunch is that the formatting tags are adding lengths to the string that need to be accounted for in the format function, but that's purely a shot in the dark right now.

Any tips on how to write the format function to pad the proper length?

  • 1
    You will probably need to use a fixed-width font like Courier.
    – Bjorn
    Jan 26, 2018 at 15:23
  • That did it! Now to find out what the heck which fonts are fixed width and which aren't
    – GIS87
    Jan 26, 2018 at 15:35

1 Answer 1


The only way I know to get multiple columns of data to line up in a single text box is to use a fixed-width font. One list of the fixed-width fonts available in Windows can be found in StackExchange's Graphic Design forum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.