str方法總結
第一組:將其變爲相應的格式
1 s.capitalize()
首字母大寫
2 s.lower()
變成小寫
3 s.upper()
變成大寫
4 s.title()
變成英文標題的格式
5 s.swapcase()
大小寫相互轉化
第二組:首尾判斷
1 s.endswith(suffix,start,end)
制定範圍內以啥啥啥開頭?
2 s.startswith(prifix,start,end)
制定範圍內以啥啥啥結尾?
第三組:類型判斷
1 s.isalnum()
數字或者字母?
2 s.isalpha()
字母?
3 s.isdigit()
數字?
4 s.islower()
小寫字母?
5 s.isupper()
大寫字母?
6 s.isspace()
空格?
7 s.istitle()
符合標題格式?
第四組:格式、內容調整
1 s.center(width,fillchar)
width指定長度,s居中,fillchar填充
2 s.ljust(width,fillchar)
width指定長度,s靠左,fillchar填充
3 s.rjustwidth,fillchar)
width指定長度,s靠右,fillchar填充
4 s.strip(char)
去除兩端空格
5 s.lstrip(char)
去左邊空格
6 s.rstrip(char)
去右邊空格
7 s.format()
格式化輸出
s = '{0}{1}{2}'.format('zero','one','two')
s = '{one}{two}{three}'.format(one = 'one', two = 'two' ,three = 'three')
第一行中,前半部分順序從0開始,按照後半部分給出的值的順序向裏填充
第二行中,相當於關鍵字參數
8 s.expandtabs(tabsize)
將字符串中的tab換成tabsize個空格
9 s.replace(old,new,count)
new替換old,count次
第五組:查找
1 s.find(sub,start,end)
在指定範圍尋找sub,找到了就返回索引,找不到返回-1,找第一個。
2 s.rfind(sub,start,end)
從右邊開始找
3 s.index(sub,start,end)
與find不同的是找不到會報錯
4 s.rindex(sub,start,end)
從右邊開始找
第六組:分割、拼接
1 s.split(sep,maxsplit)
分割字符串
2 s.splitlines(num)
按行分割
3 s.partition(sep)
變成一個3-tuple------》(前,sep,後)
4 s.join(iterable)
將iterable中的元素用s拼接起來
其餘
1 s.decode(encoding,errors)
解碼,給出編碼方式,也可以給出錯誤返回值
2 s.count(sub,start,end)
計數
s.translate()//這個還沒搞明白

浙公网安备 33010602011771号