標籤:
$ 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===================================
減號(-)用來靠左對齊數值
星號(*)解析度從表達元組讀取參數
著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。
Beginning Python From Novice to Professional (4) - 示範樣本格式字串