#字符串格式化示例
#使用给定的宽度打印格式化后的价格表
width = input ('Please enter width: ')
price_width = 10
item_width = width - price_width
head_format = '%-*s%*s'
format = '%-*s%*.2f'
print '=' * width
print head_format % (item_width,'Item',price_width,'Price')
print '-' * width
print 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 '=' * width
>>>
Please enter width: 35
===================================
Item Price
-----------------------------------
Apples 0.40
Pears 0.50
Cantaloupes 1.92
===================================
本文介绍了一个简单的Python程序,用于创建格式化的商品价格表。通过用户输入表格宽度,程序能够生成带有固定宽度列的商品名称和价格表格。示例展示了如何使用字符串格式化功能来确保表格的整洁和易读性。
105

被折叠的 条评论
为什么被折叠?



