字元串的常用操作包括但不限於以下操作: ...
字元串的常用操作包括但不限於以下操作:
字元串的替換、刪除、截取、複製、連接、比較、查找、分割等
1 #capitalize:字元串首字母大寫:capitalize-將字元串的首字母大寫,其他字母小寫 2 name = 'swhthaitun' 3 name.capitalize() 4 print(name.capitalize())
1 #casefold:字元串所有字母小寫:casefold-將字元串中所有的大寫字元轉換成小寫字元 2 name = 'HelloWorld' 3 name.casefold() #將所有語言的字元轉換為小寫 4 name.lower() #lower只對ASCII有效,對其他語言無效 5 print(name.casefold()) 6 print(name.lower())
1 #center:字元串寬度填充,使用原有字元串+填充字元構成指定長度的新的字元串 2 name = 'swhthaitun' 3 #在原有字元串兩端填充相同數目的字元 4 print(name.center(15)) 5 print(name.center(16,'*')) 6 name2 = 'HelloWorld' 7 print(name.center(20,'*'))
1 #count:統計某個字元在字元串中出現的次數,或者是在字元串的指定區間內完成上述操作 2 name = 'swhthaitun' 3 result = name.count('h') 4 print(result) #2 5 result2 = name.count('h',0,3) 6 print(result2) #1 7 name2 = 'HelloWorld' 8 result3 = name2.count('W') #不能換成'w',Python對大小寫敏感 9 print(result3) #1
1 #encode:對字元串進行編碼操作 2 name = 'swhthaitun' 3 name.encode() 4 print(name.encode()) #b'swhthaitun'
1 #endwith:判斷字元串是否以某個字元或字元串結尾,返回值為布爾值 2 name = 'swhthaitun' 3 result1 = name.endswith('s') 4 print(result1) #False 5 result2 = name.endswith('n') 6 print(result2) #True 7 result3 = name.endswith('tun') 8 print(result3) #True
1 #expandtabs:將製表符'\t'轉換成制定寬度的tab鍵分割,預設的tabsize=8 2 li = 'sw\tht' 3 li.expandtabs(4) 4 print(li.expandtabs(4)) #sw ht 5 print(li.expandtabs()) #sw ht
1 #find:在字元串中查找指定字元串,找不到事返回-1 2 name = 'swht' 3 name.find('s') 4 name.find('h') 5 name.find('a') 6 print(name.find('s'),name.find('h'),name.find('a')) #0 2 -1
1 #format:格式化輸出字元串 2 li = "I'm {},{}" #其中{}為占位符 3 result = li.format('swht','歡迎來中國') 4 print(result) #I'm swht,歡迎來中國
1 #__contains__:判斷字元串中是否包含某段字元,返回值為布爾值 2 name = 'swhtkkskjj' 3 result = name.__contains__('swht') 4 print(result) #True
1 #index:在字元串中查找指定的字元串,找不到時直接報錯 2 name = 'swhthaitun' 3 result1 = name.index('h') 4 #result2 = name.index('m') 5 print(result1) #2 6 #print(result2) #字元串中沒有'm',異常報錯
1 #join:字元串拼接 2 name = 'swhthaitun' 3 '*'.join(name) 4 name.join('ab') 5 print(name.join('ab')) #aswhthaitunb 6 print('*'.join(name)) #s*w*h*t*h*a*i*t*u*n
1 #isalnum:檢查判斷字元串是否包含字母或數字 2 name1 = 'swhthaitun' 3 name2 = '12345' 4 result1 = name1.isalnum() 5 result2 = name2.isalnum() 6 print(result1) #True 7 print(result2) #True
1 #isalpha:檢測判斷字元串是否完全由字母組成 2 name = 'swhthaitun' 3 result = name.isalpha() 4 print(result) #True
1 #isdecimal:檢查字元串是否只包含十進位字元 2 name = '123456' 3 result = name.isdecimal() 4 print(result) #True
1 # isdecimal() 2 # True: Unicode數字,,全形數字(雙位元組) 3 # False: 羅馬數字,漢字數字 4 # Error: byte數字(單位元組)
1 #isdigit:檢測字元串是否只有數字組成 2 name = '12345' 3 result = name.isdigit() 4 print(result) #True
1 # isdigit() 2 # True: Unicode數字,byte數字(單位元組),全形數字(雙位元組),羅馬數字 3 # False: 漢字數字 4 # Error: 無
1 #isidentifier:檢測字元串是否以字母開頭 2 name = 'swhthaitun' 3 result = name.isidentifier() 4 print(result) #True 5 6 name2 = '2swhthaitun' 7 result2 = name2.isidentifier() 8 print(result2) #False
1 #isnumeric:檢測字元串是否只有數字組成,這種方法只針對於unicode對象 2 name = 'swhthaitun' 3 result = name.isnumeric() 4 print(result) #False 5 6 name2 = '123545' 7 result2 = name2.isnumeric() 8 print(result2) #True
1 #isprintable:判斷字元串中所有字元是否都屬於可見字元 2 name = '\tPuppy' 3 result = name.isprintable() 4 print(result) #False 5 6 name2 = 'swhthaitun' 7 result2 = name2.isprintable() 8 print(result2) #True
1 #isspace:檢測字元串是否全部為空格 2 #istitle:檢測字元串中每一個連續的字元串的的首字母是否大寫 3 #name = 'Puppy' #True 4 #name = 'Puppy Abc' #True 5 #name = ' Puppy' #True 6 # name = 'a Puppy' #False 7 # print(name.istitle()) 8 9 #isupper:判斷字元串中所有的字母是否都是大寫字母 10 #lower:將所有字母轉換為小寫字母 11 #lstrip:去除字元串左邊開頭的空格 12 #rstrip:去除字元串右邊結尾的空格 13 #strip:去除字元串兩邊的空格
1 #maketrans:用於創建字元映射的轉換表,對於接收兩個參數的調用方式,第一個參數是字元串,表示需要轉換的字元 2 #第二個參數表示字元串需要轉換的目標(沒怎麼看懂) 3 intab = 'swhtr' 4 outtab = '12345' 5 name = 'swhtr hjjksknsnjmk' 6 print(name.maketrans(intab,outtab)) #{115: 49, 119: 50, 104: 51, 116: 52, 114: 53}
1 #partition:根據指定的分隔符對字元串進行分割 2 name = 'ls' 3 li = 'hhsslswhtolljm' 4 result = li.partition(name) 5 print(result) #('hhss', 'ls', 'whtolljm')
1 #replace:將字元串中的old(舊字元串)替換成new(新字元串),如果指定第三個參數max,則替換不超過max次 2 '''語法:str.replace(old, new[, max]) 3 參數:old -- 將被替換的子字元串。 4 new -- 新字元串,用於替換old子字元串。 5 max -- 可選字元串, 替換不超過 max 次''' 6 str = 'this is string example.....wow!!! this is really string' 7 str_new = str.replace('is','was') 8 str_new2 = str.replace('is','was',3) 9 print(str_new) #thwas was string example.....wow!!! thwas was really string 10 print(str_new2) #thwas was string example.....wow!!! thwas is really string
1 #split:字元串分割,預設是空格, 2 #語法:str.split(str="", num=string.count(str)). 3 #str -- 分隔符,預設為所有的空字元,包括空格、換行(\n)、製表符(\t)等。num--分割次數 4 name = 'swhthaitun' 5 result = name.split('h') 6 result2 = name.split('h',1) 7 print(result) #['sw', 't', 'aitun'] 8 print(result2) #['sw', 'thaitun']
1 #__add__:在字元串後面增加指定的字元或者字元串 2 name = 'swht' 3 result = name.__add__('e') 4 print(result) #swhte 5 li = 'hjh' 6 result2 = name.__add__(li) 7 print(result2) #swhthjh
1 #__eq__:判斷字元串是否相等,返回值為布爾 2 name = 'swht' 3 li = 'test' 4 result = name.__eq__(li) 5 print(result) #False
# isdecimal()
# True: Unicode數字,,全形數字(雙位元組)
# False: 羅馬數字,漢字數字
# Error: byte數字(單位元組)