Print formatting

D'Arcy Cain darcy at VybeNetworks.com
Fri Oct 18 17:00:02 EDT 2019


On 10/18/19 2:21 PM, Jagga Soorma wrote:
> I seem to have found a way to do this with the following:
> 
>   print('{:<12s}{:>12s}'.format((temp_list[0]),(temp_list[3])))
> 
> Still let me know if there is a better way to format this output :)

I would start with removing the redundant parens.

print('{:<12s}{:>12s}'.format(temp_list[0],temp_list[3]))

But then I would simplify it further.

print('{0[0]:<12s}{0[3]:>12s}'.format(temp_list))

You can do a similar thing with dictionaries.

print('{0[data0]:<12s}{0[data3]:>12s}'.format(temp_dict))

You can even mix and match.

print('{0[0]:<12s}{1[data3]:>12s}'.format(temp_list, temp_dict))

Finally, if this is in a loop do this.

FMT = '{0[0]:<12s}{0[3]:>12s}'.format
for temp_list in GetLists(): print FMT(temp_list)

Cheers.

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:darcy at Vex.Net VoIP: sip:darcy at VybeNetworks.com



More information about the Python-list mailing list