#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)
Python 2.7.11 字符串操作
最新推荐文章于 2022-09-13 21:11:56 发布