python格式化输出宽度对齐_python 中 字符串格式化示例,输出格式没有达到指定宽度,求帮忙!...

python 版本:2.7.3

运行平台:macX 10.8

开发工具:IDLE

问题描述

width = input('Please enter width(>10): ')

price_width = 10

item_width = width - price_width

header_format = '%-*s%*s'

item_format = '%-*s%*.2f'

print '=' * width

print header_format % (item_width,'Item',price_width,'Price')

print '-' * width

print item_format % (item_width,'Apples',price_width,0.4)

print item_format % (item_width,'Apples',price_width,0.5)

print item_format % (item_width,'Cantaloupes',price_width,1.92)

print '=' * width

运行结果:

image.tiff

书的结果是对齐的,为什么我这边显示是错乱的,

ps:(刚在windows下运行同样的脚本,发现显示是正常的 - -!)

image.tiff

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值