$ gedit price.py
#!/usr/bin/env pythonwidth = input (' Please enter width: ') price_width = 10item_width = Width-price_widthheader_format = '%-*s%*s ' format = '%-*s%*.2f ' print ' = ' * widthprint header_format% (item_width, ' item ', Price_width, ' price ') print ' -' * widthprint format% (item_width, ' Apples ', price_width,0.4) print format% (item_width, ' Pears ', price_width,0.5) print Format% (item_width, ' cantaloupes ', price_width,1.92) print format% (item_width, ' apricots ', price_width,8) print format % (Item_width, ' prunes ', price_width,12) print ' = ' * width
$ python price.py
Please enter Width:35===================================item price -----------------------------------Apples 0.40Pears 0.50Cantaloupes 1.92Apricots 8.00Prunes 12.00================================== =
Minus sign (-) to align the values to the left
Asterisk (*) resolution reads parameters from the expression tuple
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Beginning Python from Novice to Professional (4)-Demo sample format string