1. __add__:字符串拼接python
【示例】:
>>> str1=‘good’
>>> str1.__add__(‘morning’)
>>> ‘goodmorning’
2. __contains__:判斷字符串中是否包含某字符,是則返回Trueios
【示例】: >>> str1='good' >>> str1.__contains__('od') >>> True
>>> str1.__contains__('ods')
>>> False
3. __eq__:判斷兩者是否相等,是則返回Truegit
【示例】: >>> str1='good' >>> str1.__eq__('good') >>> True >>> str1.__eq__('goods') >>> False
4. __ge__:判斷self>=value,是則返回Trueapi
【示例】: >>> str1='s' >>> str1.__ge__('t') >>> False >>> str1.__ge__('a') >>> True
5. __gt__:判斷self>value,是則返回True安全
6. __le__:判斷self<=value,是則返回Truethis
7. __lt__:判斷self<value,是則返回True編碼
8. capitalize():將字符串首字母轉換成大寫spa
【示例】: >>> str1='good' >>> str1.capitalize() >>> 'Good'
9. casefold():將字符串首字母轉換成小寫rest
【示例】: >>> str1='Good' >>> str1.casefold() >>> 'good'
10. center():居中顯示,其他用指定字符填充,參數1:字符總長度,參數2:填充字符code
【示例】: >>> str1='good' >>> str1.center(20,'*') >>> '********good********'
11. count():統計字符個數
【示例】: >>> str1='good good study,day day up' >>> str1.count('d') >>> 5 >>> str1.count('d',0,12) #空格也佔用1位 >>> 2
12. encode():轉換編碼格式
【示例】: >>> str1='安全' >>> str1.encode('gbk') #由utf-8轉換成gbk編碼格式 >>> b'\xb0\xb2\xc8\xab'
13. endswith():判斷結尾是不是否正確,返回bool值
【示例】: >>> str1='good good study,day day up' >>> str1.endswith('dy',0,15) >>> True >>> str1.endswith('ups') >>> False
14. expandtabs():把字符串中的tab符號(\t)轉換成空格,\t默認是8個空格,能夠指定空格長度
【示例】: >>> str1='good\tup' >>> str1.expandtabs() >>> 'good up' #從頭開始計數,默認8位,不足用空格填充 >>> len(str1.expandtabs()) >>> 10 >>> str1.expandtabs(10) #從頭開始計數,指定10位,不足用空格填充 >>> 'good up' >>> len(str1.expandtabs(10)) >>> 12
15. find():返回字符串索引位置
【示例】: >>> str1='good morning' >>> str1.find('od') >>> 2 >>> str1.find('o',0,2) >>> 1 >>> str1.find('o',0,3) #str1[1]和str1[2]值均爲'o',返回第一個'o'的索引位置 >>> 1
16. index():返回字符串索引位置,相似於find()
區別:find()查找不到指定字符串時,會返回None,而index()會報錯
17. format():字符串格式化
【示例1】: >>> str1='good {0} {1}' >>> str1.format('day','everyone') >>> 'good day everyone' 【示例2】: >>> str1='Tom is a {sex}' >>> str1.format(sex='boy') >>> 'Tom is a boy'
18. isalnum():判斷是否爲字母或數字,返回bool值
【示例1】 >>> str1='helloworld' >>> str1.isalnum() >>> True 【示例2】 >>> str2=‘123’ >>> str2.isalnum() >>> True 【示例3】 >>> str3='hello world' >>> str3.isalnum() >>> False
19. isalpha():判斷是否全爲字母,返回bool值
【示例1】: >>> str1='helloworld' >>> str1.isalpha() >>> True 【示例2】: >>> str2='hello1' >>> str2.isalpha() >>> False
20. isdecimal():判斷是否全爲十進制字符,返回bool值
【示例1】: >>> str1=‘1234567890’ >>> str1.isdecimal() >>> True 【示例2】: >>> str2='a123' >>> str2.isdecimal() >>> False
21. isdigit():判斷是否全爲數字,返回bool值
【示例1】: >>> str1=‘123’ >>> str1.isdigit() >>> True 【示例2】: >>> str2=' ' >>> str2.isdigit() >>> False
22. islower():判斷是否全爲小寫字母,返回bool值
【示例1】 >>> str1='abc' >>> str1.islower() >>> True 【示例2】 >>> str2='Good' >>> str2.islower() >>> False
23. isnumeric():判斷是否全爲數字,返回bool值
【示例1】 >>> str1='123' >>> str1.isnumeric() >>> True 【示例2】 >>> str2='123a' >>> str2.isnumeric() >>> False
24. isprintable():判斷是否只包含可打印字符,返回bool值
【示例1】 >>> str1='hello world@123' >>> str1.isprintable() >>> True 【示例2】 >>> str2='abc\t124' >>> str2.isprintable() >>> False
25. isspace():判斷是否只包含空格字符,返回bool值
【示例1】 >>> str1=' ' >>> str1.isspace() >>> True 【示例2】 >>> str2='a ' >>> str2.isspace() >>> False
26. istitle():判斷是否每一個詞的首字母均爲大寫,返回bool值
【示例1】 >>> str1='Hello World!' >>> str1.istitle() >>> True 【示例2】 >>> str2='Hello world' >>> str2.istitle() >>> False
27. isupper():判斷字符串是否全爲大寫字母,返回bool值
【示例1】 >>> str1='GOOD' >>> str1.isupper() >>> True 【示例2】 >>> str2='GOOd' >>> str2.isupper() >>> False
28. join():將字符串分割成單個字符,且用指定字符進行拼接,並返回新的字符串
【示例】 >>> str1='helloworld' >>> '_'.join(str1) >>> 'h_e_l_l_o_w_o_r_l_d'
29. ljust():左對齊,使用指定字符進行填充,默認使用空格填充
【示例1】 >>> str1='hello' >>> str1.ljust(10) >>> 'hello ' 【示例2】 >>> str2='hello' >>> str2.ljust(10,'*') >>> 'hello*****'
30. rjust():右對齊,使用指定字符填充,默認使用空格填充
【示例】 >>> str1='hello' >>> str1.rjust(10,'*') >>> '*****hello'
31. zfill():右對齊,若指定長度<=字符串自身長度,則返回字符串自身,反之,默認使用0填充
【示例】 >>> str1='hello' >>> str1.zfill(10) >>> '00000hello'
32. lower():將大寫字母所有轉換成小寫
【示例】 >>> str1='HELLO' >>> str1.lower() >>> 'hello'
33. lstrip():清除字符左邊的空格
【示例】 >>> str1=' hello ' >>> str1.lstrip() >>> 'hello '
34. rstrip():清除字符串右邊的空格
>>> str1=' hello ' >>> str1.rstrip() >>> ' hello'
35. strip():清除字符串兩邊的空格
【示例】 >>> str1=' hello ' >>> str1.strip() >>> 'hello'
36. maketrans():與translate配合使用,對應替換相應字符
【示例】 >>> str1='this is a testing example,very interesting' >>> test1='aeios' >>> test2='12345' >>> test3=str1.maketrans(test1,test2) >>> str1.translate(test3) >>> 'th35 35 1 t25t3ng 2x1mpl2,v2ry 3nt2r25t3ng'
37. partition():使用指定字符分割字符串,返回一個由3個元素組成的元組,3個元素分別是指定字符先後字符串以及指定字符自己
若指定字符不存在於字符串中,則返回原字符串及2個空元素
【示例1】 >>> str1='hello world! go on' >>> str1.partition('d') >>> ('hello worl','d','! go on') 【示例2】 >>> str1='hello world! go on' >>> str1.partition('z') >>> ('hello world! go on','','')
38. rpartition():使用指定字符從右分割字符串,返回一個由3個元素組成的元組,3個元素分別是指定字符先後字符串以及指定字符自己,與partition()相反
若指定字符不存在於字符串中,則返回原字符串及2個空元素
【示例1】 >>> str1='hello world! go on' >>> str1.rpartition('o') >>> ('hello world! go ','o','n') 【示例2】 >>> str1='hello world! go on' >>> str1.rpartition('z') >>> ('','','hello world! go on')
39. replace():使用新字符替換舊字符,可指定替換個數,默認替換所有
【示例1】 >>> str1='hello world! go on' >>> str1.replace('o','8',2) >>> 'hell8 w8rld! go on'
40. rfind():從字符串右側開始查找,返回字符索引位置,與find()相反
【示例】 >>> str1='hello world! go on' >>> str1.rfind('o') >>> 16
41. rindex():從字符串右側開始查找,返回字符索引位置,與index()相反,相似rfind()
42. split():從左側拆分字符串,返回一個列表類型
1:默認分隔符爲空格:
【示例1】 #默認分割符爲空格 >>> str1='hello world! go on' >>> str1.split() >>> ['hello','world!','go','on']
2: 分隔符不能爲空('')
3:若沒有分割符,則將整個字符串做爲列表的一個元素輸出
【示例】 >>> str1='helloworld' >>> str1.split() >>>['helloworld']
4: 指定分割次數
【示例】 >>> str1='hello world! go on' >>> str1.split(' ','2') #使用空格進行分割,分割次數爲2,則返回N+1即3個元素 >>> ['hello','world!','go on']
5. 特殊狀況:出現多個分割符鏈接在一塊兒
分割思路:分隔符分割字符串時,分割符爲x,則將x左側的字符串放入列表中,若x左側除x外無其餘字符串,則返回空''做爲列表的一個元素,繼續分割x右側剩餘的字符串,以此類推
第一次分割------>['','xhelloxxxworldxxxxgox']
第二次分割------>['','','helloxxxworldxxxxgox']
第三次分割------>['','','hello','xxworldxxxxgox']
第四次分割------>['','','hello','','xworldxxxxgox']............以此類推
43. rsplit():從右側開始分割,與split()相反
【示例】 >>> str1='hello world! go on' >>> str1.rsplit('g') >>> ['helloworld! ','o on']
44. splitlines():分割多行字符串,將每行字符串做爲列表的一個元素
keepends 值決定輸出結果中是否保留換行符,默認爲 False,不包含換行符,若爲 True,則保留換行符
1. 1. 多行字符串,默認分割
2. keepends值爲True,進行分割
45. startswith():判斷開頭字符是否正確,返回bool值
【示例1】 >>> str1='hello world! go on' >>> str1.startswith('he') >>> True 【示例2】 >>> str1='hello world! go on' >>> str1.startswith('wo',0,6) >>> True
46. swapcase():將字符串中的大小寫字母互轉
【示例】 >>> str1='Hello World' >>> str1.swapcase() >>> 'hELLO wORLD'
47. title():字符串中每一個單詞首字母大寫
【示例】 >>> str1='hello world! go on' >>> str1.title() >>> 'Hello World! Go On'
48.upper():將字符串全轉換爲大寫字母,與lower()相反
【示例】 >>> str1='hello world!' >>> str1.upper() >>> 'HELLO WORLD!'
49. __hash__:若是對象object爲哈希表類型,返回對象object的哈希值。哈希值爲整數。在字典查找中,哈希值用於快速比較字典的鍵。兩個數值若是相等,則哈希值也相等,等於hash(values)
50. __init__:str構造方法,操做a='s' or a=str('s')自動調用
51. __len__:返回字符串長度
【示例】: >>> str1='student' >>> str1.__len__() >>> 7
52. __getattribute__:反射會用到