3.2 字符串的常見操作

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,表示沒有找到。

  1. __sub:要查找的數(shù)據(jù)
  2. __start:開始下標
  3. __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ù)不存在,則程序崩潰。

  1. __sub:要查找的數(shù)據(jù)
  2. __start:開始下標
  3. __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ù)范圍。

  1. x:要統(tǒng)計的數(shù)據(jù)
  2. __start:開始下標
  3. __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)用后會返回一個替換后的字符串。

示例:

  1. __old:舊的字符串
  2. __new:新的字符串(替換后的字符串)
my_str = "hello"
new_str = my_str.replace("l", "x")
print("替換后新的字符串為:", new_str)     # 結(jié)果是:替換后新的字符串為: hexxo
  1. __old:舊的字符串
  2. __new:新的字符串(替換后的字符串)
  3. __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表示分割一次。

示例:

  1. sep:根據(jù)指定數(shù)據(jù)進行分割,返回的是一個列表數(shù)據(jù)
  2. 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)

striplstriprstrip):去除兩邊(左邊、右邊)。
空白字符:空格、\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ù)必須是字符串類型才可以,因為返回的是一個字符串。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容