Python字符串应用(1)打印价格列表

书上的例子
width = input('please enter width:')

priceWidth = 10
itemWidth = width - priceWidth
headerFormat = '%-*s%*s' %标记转换说明符的开始,-表示左对齐,转换后字符串至少应该具有指定宽度 *从值元组中读出 
       s表示字符串(字符串格式化转换类型) 
format       = '%-*s%*.2f'

print '=' * width
print headerFormat % (itemWidth,'Item',priceWidth,'Price')
print '=' * width

print format % (itemWidth,'apples',priceWidth,0.4)
print format % (itemWidth,'oranges',priceWidth,0.5)
print format % (itemWidth,'pears',priceWidth,1.2)

print '=' * width
 
  

please enter width:35
===================================
Item                          Price
===================================
apples                         0.40
oranges                       0.50
pears                          1.20
===================================

你可能感兴趣的:(Python字符串应用(1)打印价格列表)