Python 2.7.11 字符串操作

#coding=utf-8
# StringOperation.py

# 1. str.find(substr,startIdx,endIdx)
# 返回左边第一个子串的左边第一个字符的索引
# 区分大小写
string1='To be or not to be'
print string1.find('be',1)
print string1.find('to',0,5)#未找到返回-1

# 2. sep.join(sequence)
# sequence 元素间插入sep, not in-place
sep='+'
seq=range(1,10)
print seq
print sep.join(seq)# cannot be list(int)
sep='+'
seq=['1','2','3']
print seq
print sep.join(seq)# string list
seq='To be or not to be'
print sep.join(seq)# string
# 3. lower 小写
string1='To be or not to be'
print string1.lower()
# 4. replace 字串替换
print string1.replace('be','BE')
print string1# 不是in-place
# 5. split 分割字符串,,默认以空格为分隔符
sep='+'
seq=['1','2','3']
seq=sep.join(seq)
print seq
print seq.split(sep)
# 6. strip 除去两侧空格
seq='       To be or not to be       '
print seq
print seq.strip()
print seq.lstrip()# 只去左边空格
print seq.rstrip()# 只去右边空格
# 7. translate 批量替换字符
from string import maketrans
table=maketrans('bn','BN')
print len(table)
print table[97:123]#转换表
print maketrans('','')[97:123]#不转换的转换表
print seq.translate(table)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值