Python 列表(List)操作方法详解
Python 字典(Dictionary)操作详解
strip lstrip rstrip
传入的是一个字符数组,编译器去除两端所有相应的字符,直到没有匹配的字符,比如:
代码如下: theString = 'saaaay yes no yaaaass' print theString.strip('say')
theString依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。所以,输出的结果为:
yes no 比较简单吧,lstrip和rstrip原理是一样的。注意:当没有传入参数时,是默认去除首尾空格的。s.strip().lstrip().rstrip(',') //组合使用
index
s.index('o') //返回第一个 ‘o’ 字符串所在位置
cmp()比较字符串
sStr1 = 'strchr'
sStr2 = 'strch'print cmp(sStr1,sStr2) //返回值 -1 0 1将字符串中的大小写转换
S.lower() #小写
S.upper() #大写 S.swapcase() #大小写互换 S.capitalize() #首字母大写翻转字符串
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1]print sStr1查找字符串
sStr1 = 'abcdefg'
sStr2 = 'cde'print sStr1.find(sStr2)分割字符串
s = 'ab,cde,fgh,ijk'
print(s.split(','))连接字符串
delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']print delimiter.join(mylist)字符串在输出时的对齐
S.ljust(width,[fillchar])
#输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格。 S.rjust(width,[fillchar]) #右对齐 S.center(width, [fillchar]) #中间对齐 S.zfill(width) #把S变成width长,并在右对齐,不足部分用0补足字符串的分割和组合
S.split([sep, [maxsplit]])
#以sep为分隔符,把S分成一个list。maxsplit表示分割的次数。默认的分割符为空白字符 S.rsplit([sep, [maxsplit]]) S.splitlines([keepends]) #把S按照行分割符分为一个list,keepends是一个bool值,如果为真每行后而会保留行分割符。 S.join(seq) #把seq代表的序列──字符串序列,用S连接起来