3.2.1 find
find:根據(jù)指定數(shù)據(jù)獲取對應(yīng)的下標。
提示:如果數(shù)據(jù)不存在,則返回-1,表示沒有找到。
示例:
my_str = "abcdef" index = my_str.find("c") print("下標為:", index) # 結(jié)果是:下標為: 2新用法:
find方法還可以指定數(shù)據(jù)范圍,如果數(shù)據(jù)不存在,則返回-1,表示沒有找到。
__sub:要查找的數(shù)據(jù)__start:開始下標__end:結(jié)束下標(不包含結(jié)束下標)index = my_str.find("c", 0, 2) print("下標為:", index) # 結(jié)果是:下標為: -1擴展:
my_str = "hello" # find 表示從左往右查找對應(yīng)數(shù)據(jù)的下標 index = my_str.find("l") print("下標為:", index) # 結(jié)果是:下標為: 2 # rfind 表示從右往左查找對應(yīng)數(shù)據(jù)的下標 index = my_str.rfind("l") print("下標為:", index) # 結(jié)果是:下標為: 3
3.2.2 index
index:根據(jù)指定數(shù)據(jù)獲取對應(yīng)的下標。
提示:如果數(shù)據(jù)不存在則崩潰。
示例:
my_str = "hello" index = my_str.index("e") print("下標為:", index) # 結(jié)果是:下標為: 1 my_str = "hello" index = my_str.index("x") print("下標為:", index) # 結(jié)果報錯:ValueError: substring not found 即沒有發(fā)現(xiàn)指定字符串新用法:
index方法還可以指定數(shù)據(jù)范圍,如果數(shù)據(jù)不存在,則程序崩潰。
__sub:要查找的數(shù)據(jù)__start:開始下標__end:結(jié)束下標(不包含結(jié)束下標)index = my_str.index("l", 0, 3) print("下標為:", index) # 結(jié)果是:下標為: 2
3.2.3 count
count:根據(jù)指定數(shù)據(jù)統(tǒng)計該數(shù)據(jù)出現(xiàn)的次數(shù)。
示例:
my_str = "abccba" result = my_str.count("a") print("次數(shù)為:", result) # 結(jié)果是:次數(shù)為: 2新用法:
count方法還可以指定數(shù)據(jù)范圍。
x:要統(tǒng)計的數(shù)據(jù)__start:開始下標__end:結(jié)束下標(不包含結(jié)束下標)result = my_str.count("a", 0, 2) print("次數(shù)為:", result) # 結(jié)果是:次數(shù)為: 1
3.2.4 replace
replace:根據(jù)指定數(shù)據(jù)對字符串的數(shù)據(jù)進行替換。
提示:replace方法調(diào)用后會返回一個替換后的字符串。
示例:
__old:舊的字符串__new:新的字符串(替換后的字符串)my_str = "hello" new_str = my_str.replace("l", "x") print("替換后新的字符串為:", new_str) # 結(jié)果是:替換后新的字符串為: hexxo
__old:舊的字符串__new:新的字符串(替換后的字符串)__count:表示替換的次數(shù),不指定表示全部替換new_str = my_str.replace("l", "x", 1) print("替換后新的字符串為:", new_str) # 結(jié)果是:替換后新的字符串為: hexlo
3.2.5 split
split:根據(jù)指定數(shù)據(jù)對字符串進行分割。
分割次數(shù):默認是-1,表示全部分割,1表示分割一次。
示例:
sep:根據(jù)指定數(shù)據(jù)進行分割,返回的是一個列表數(shù)據(jù)maxsplit:表示最大的分割次數(shù),不指定表示全部分割my_str = "A:B:C" result = my_str.split(":", 1) print(result, type(result)) # 結(jié)果是:['A', 'B:C'] <class 'list'>
split擴展:不指定參數(shù)表示可以根據(jù)空白字符串進行分割(空白字符串:空格,\n, \t)。my_str = "a b\nc\td" result = my_str.split() print(result, type(result)) # 結(jié)果是:['a', 'b', 'c', 'd'] <class 'list'>
3.2.6 startswith(endswith)
startswith(endswith):表示判斷是否是以指定的字符串開頭(結(jié)尾)。
示例:
my_str = "http://www.baidu.com"
startswith:表示判斷是否是以指定的字符串開頭。result = my_str.startswith("http") print(result) # 結(jié)果是:True
endswith:表示判斷是否是以指定字符串結(jié)尾。result = my_str.endswith("cn") print(result) # 結(jié)果是:False result = my_str.endswith("com") print(result) # 結(jié)果是:True
3.2.7 strip(lstrip、rstrip)
strip(lstrip、rstrip):去除兩邊(左邊、右邊)。
空白字符:空格、\n、\t
示例:
my_str = " abc " print(my_str) # 結(jié)果是: abc
strip:去除兩邊空格result = my_str.strip() print(result) # 結(jié)果是:abc
lstrip:只去除左邊的空格result = my_str.lstrip() print(result) # 結(jié)果是:abc
rstrip:只去除右邊的空格result = my_str.rstrip() print(result) # 結(jié)果是: abc擴展:
strip還可以去除兩邊的指定數(shù)據(jù)my_str = "!abc!" new_str = my_str.strip("!") print(new_str) # 結(jié)果是:abc
3.2.8 partition
partition:表示根據(jù)指定數(shù)據(jù)把字符串分割成三部分。
my_str = "1.txt"
result = my_str.partition(".")
print(result, type(result)) # 結(jié)果是:('1', '.', 'txt') <class 'tuple'>
rpartition:表示根據(jù)指定數(shù)據(jù)從右往左找指定的數(shù)據(jù)把字符串分割成三部分。
my_str = "1.txt.png"
result = my_str.rpartition(".")
print(result, type(result)) # 結(jié)果是:('1.txt', '.', 'png') <class 'tuple'>
3.2.9 isdigit
isdigit:表示判斷字符串里面的數(shù)據(jù)是否都是整型數(shù)據(jù)。
my_str = "80"
result = my_str.isdigit()
print(result) # 結(jié)果是:True
3.2.10 join
join:根據(jù)指定數(shù)據(jù)把容器類型中的每一個數(shù)據(jù)按照字符串進行拼接,返回一個新的字符串。
示例:
my_str = "abc" result = "_".join(my_str) print(result, type(result)) # 結(jié)果是:a_b_c <class 'str'>使用
join還可以對列表中的每個數(shù)據(jù)按照字符串的方式進行拼接。my_list = ["1", "2", "3"] result = "#".join(my_list) print(result, type(result)) # 結(jié)果是:1#2#3 <class 'str'>提示:使用
join方法的時候,容器類型中的每個數(shù)據(jù)必須是字符串類型才可以,因為返回的是一個字符串。