您的位置:首页 > 其它

格式化字符串-学习笔记

2016-07-03 20:37 162 查看
# 使用给定的宽度打印格式化后的价格列表


width = int(input('please enter width(>= 32):'))
price_width = 10
item_width = width - price_width

header_format = '%-*s%*s'
format = '%-*s%*.2f'
# 用-号右对齐,*号用来引用元组中参数.
print('=' * width)

print(header_format % (item_width,'item',price_width,'prince'))
# item_with和prince_width引用header_format各自格式化了后面的字符串.在下面则是引用format.

print('-' *width)

print(format % (item_width,'Apple',price_width,0.4))
print(format % (item_width,'Pears',price_width,0.5))
print(format % (item_width,'Cantal oupes',price_width,1.92))
print(format % (item_width,'Dried Apricots(16 oz.)',price_width,8))
print(format % (item_width,'Prunes(4 lbs)',price_width,12))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: