4.Python字符串
字符串是python中重要的數據對象,單引號,雙引號以及三個單引號包含的任意python的數據對象都稱爲字符串。
5.Python字符串方法
字符串填充:
- center(width[,fillchar]) 居中對齊
In [1]: a='tcq'
In [2]: a.center(10,'*')
Out[2]: '***tcq****'
- ljust(width[,fillchar])左對齊
In [3]: a.ljust(10,'-')
Out[3]: 'tcq-------'
- rjust(width[,fillchar])右對齊
In [4]: a.rjust(10,'=')
Out[4]: '=======tcq'
- zfill(width)0比特填充
In [5]: a.zfill(10)
Out[5]: '0000000tcq'
- expandtabs([tabsize])默認爲8,tab轉換的空格數
字符串刪減:
- strip()去除字符串兩端空白符
In [6]: a=" tcq "
In [7]: a.strip()
Out[7]: 'tcq'
- strip([chars])
In [8]: a='++tcq++'
In [9]: a.strip('+')
Out[9]: 'tcq'
- rstrip([chars])去除右端字符
In [10]: a.rstrip('+')
Out[10]: '++tcq'
- lstrip([chars])去除左端字符
In [11]: a.lstrip('+')
Out[11]: 'tcq++'
字符串變形:
- lower()將字符串轉換成小寫
In [12]: a="TCQws"
In [13]: a.lower()
Out[13]: 'tcqws'
- upper()將字符串轉換爲大寫
In [14]: a.upper()
Out[14]: 'TCQWS'
- capialize()僅首字母大寫
In [15]: a.capitalize()
Out[15]: 'Tcqws'
- swapcase()大小寫轉換
In [16]: a.swapcase()
Out[16]: 'tcqWS'
- title()單詞首字母大寫
In [17]: a="tian cai qi"
In [17]: a.title()
Out[17]: 'Tian Cai Qi'
字符串切分:
將字符串切分成了列表
- splitlines([keepends])按行切分字符串
In [18]: a="""1
22
333
"""
In [19]: a.splitlines()
Out[19]: ['1', '22', '333']
- split([sep[,maxsplit]])以sep爲分隔符,maxsplit爲分成的最大塊數。
In [20]: a="My name is TCQ "
In [21]: a.split()
Out[21]: ['My', 'name', 'is', 'TCQ']
In [22]: a.split(' ',2)
Out[22]: ['My', 'name', 'is TCQ ']
- rsplit([sep[,maxsplit]])從右側分起
In [23]: a.rsplit(' ',2)
Out[23]: ['My name is TCQ ', '', '']
字符串連接:
- join(seq)
join()函數的高效率(相對與循環),使它稱爲最值得關注的字符串方法之一。
In [24]: a='111'
In [24]: a.join('aaa')
Out[24]: 'a111a111a'
字符串判斷:
- isalnum()是否是由字母和數字組成
In [25]: a='123abc'
In [26]: a.isalnum()
Out[26]: True
In [27]: a='*123abc'
In [28]: a.isalnum()
Out[28]: False
- isalpha()是不是由純字母組成
In [29]: a='tcq'
In [30]: a.isalpha()
Out[30]: True
- isdigit()是否是純數字組成
In [31]: a='123467'
In [32]: a.isdigit()
Out[32]: True
- islower()沒有大寫字母
In [33]: a='TCQws'
In [34]: a.islower()
Out[34]: False
- isupper()沒有小寫字母
In [35]: a='12A3'
In [36]: a.isupper()
Out[36]: True
- isspace()無空格
In [37]: a=' 1 '
In [38]: a.isspace()
Out[38]: False
- istitle()單詞首字母大寫
In [39]: a='I Am 24 Years Old!'
In [40]: a.istitle()
Out[40]: True
- startswith(prefix[,start[,end]])以某字符開頭
In [41]: a='TCQws'
In [42]: a.startswith('TCQ',0,4)
Out[42]: True
- endswith(suffix[,start[,end]])以某字符結尾
In [43]: a.endswith('ws',0,5)
Out[43]: True
字符串查找:
- count(sub[,start[,end]])統計字符串中字符的個數
In [44]: a='abbcccdddd'
In [45]: a.count('d',7,10)
Out[45]: 3
- find(sub[,start[,end]])查找字符所在索引位置
In [46]: a.find('d')
Out[46]: 6
In [47]: a.find('e')
Out[47]: -1
- index(sub[,start[,end]])同find,單當找不到值時顯示‘substring not found’
字符串替換:
- replace(old,new[,count])替換字符串中的字符
In [48]: a='11111'
In [49]: a.replace('1','2',3)
Out[49]: '22211'
- translate(table[,deletechars])
translate()的參數table可以由string.maketrans(frm,to)生成translate()對unicode對象的支持並不完善,及那以不要使用
字符串編碼:
- encode([encoding[,errors]])
- decode([encoding[,errors]])
unicode可以通過encode編碼稱爲特定編碼的str,而str也可以通過decode解碼稱爲unicode
字符串分片:
字符串是有序序列,索引開始爲0,結束爲-1
In [50]: "TCQws"[::]
Out[50]: 'TCQws'
In [51]: "TCQws"[0::2](2爲步長)
Out[51]: 'TQs'