Links
Snippets
Ref: Fancier Output Formatting
Thousands separator
'{:,}'.format(1234567890) # => '1,234,567,890'
Printing floating point numbers
# 2 decimal places print 'The value of PI is approximately {0:.2f}.'.format(math.pi)
Alignment
print '{:<30}'.format('left aligned') # => 'left aligned ' print '{:>30}'.format('right aligned') # => ' right aligned' print '{:^30}'.format('centered') # => ' centered ' # use '*' as a fill char print '{:*^30}'.format('centered') # => '***********centered***********'
Nesting arguments
for align in '<^>': print '{0:{fill}{align}16}'.format(text, fill='-', align=align) # Output # # left------------ # -----center----- # -----------right for base in 'dXob': print '{0:{width}{base}}'.format(10, base=base, width=5), # Output # # 10 A 12 1010