(8)字串的操作
阿新 • • 發佈:2019-01-13
1、查詢字串:
#查詢不存在的字元,報錯
str.index(a)
#查詢不存在的字元,返回-1
str.find(a)
2、是否包含指定字串
in or not in
return bool
三、字串的長度
len(a)
四、大小寫轉換
str.lower() #轉小寫
str.uppre() #轉大寫
str.capitalize() #首字母大寫
str.swapcase() #大小寫轉換
五、剔除字串符號
str.strip() #刪除字串兩邊的指定字元,無引數預設為空格 str.rstrip() #刪除字串右邊的指定字元 str.lstrip() #刪除字串左邊的指定字元
六、複製字串
七、連線字串
a+b
a.join(b) #迭代拼接
八、 字串的切片
九、 字串的分割
str.split(sep,maxsplit)
十、字串的替換
str.replace(old,new,maxreplace)
十一、位置定位
a.center()
十二、字串的統計
a.count()
十三、字串測試判斷函式
s.startswith(prefix[,start[,end]]) #是以prefix開頭 s.endswith(prefix[,start[,end]]) #是以prefix結尾