python学习第三章----字符串

1.    
#使用给定宽度打印格式化后的价格列表
width = int(input('Please enter width:'))

price_width = 10
item_width = width - price_width

header_format = '% - *s%*s'                  <span style="background-color: rgb(204, 204, 204);"> <span style="color:#ff0000;">宽度是用户输入信息,不能硬编码,用*可以从转换元组中读出字段宽度,-表示左对齐数值 </span></span>
format1        = '% - *s%*.2f'

print('=' * width)

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

print('-' * width)

print(format1 % (item_width, 'Apple', price_width, 0.4))
print(format1 % (item_width, 'Pears', price_width, 0.5))
print(format1 % (item_width, 'Cantaloupes', price_width, 1.92))
print(format1 % (item_width, 'Dried Apricots(16 oz.)', price_width, 8))
print(format1 % (item_width, 'Prunes(4 lbs.)', price_width, 12))

print('=' * width)

2.   join (连接序列中的元素) ,  lower, replace(替换) , split(join的逆方法), strip(去除两侧空格), translate( 单个字符的替换)

3.  string.capwords(s[, sep])   使用split函数分割字符串s(以sep为分隔符),使用capitalize函数将分割得到的各单词首字母大写,并且使用join函数以sep为分隔符将个单词连接起来

string。maketrans(from,to) 创建用于转换的转化表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值