Python學習-字串操作,python學習字串
字串的常用操作包括但不限於以下操作:
字串的替換、刪除、截取、複製、串連、比較、尋找、分割等
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) #25 result2 = name.count('h',0,3)6 print(result2) #17 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) #False5 result2 = name.endswith('n')6 print(result2) #True7 result3 = name.endswith('tun')8 print(result3) #True
1 #expandtabs:將定位字元'\t'轉換成制定寬度的tab鍵分割,預設的tabsize=82 li = 'sw\tht'3 li.expandtabs(4)4 print(li.expandtabs(4)) #sw ht5 print(li.expandtabs()) #sw ht
1 #find:在字串中尋找指定字串,找不到事返回-12 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) #26 #print(result2) #字串中沒有'm',異常報錯
1 #join:字串拼接2 name = 'swhthaitun'3 '*'.join(name)4 name.join('ab')5 print(name.join('ab')) #aswhthaitunb6 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) #True7 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) #True5 6 name2 = '2swhthaitun'7 result2 = name2.isidentifier()8 print(result2) #False
1 #isnumeric:檢測字串是否只有數字組成,這種方法只針對於unicode對象2 name = 'swhthaitun'3 result = name.isnumeric()4 print(result) #False5 6 name2 = '123545'7 result2 = name2.isnumeric()8 print(result2) #True
1 #isprintable:判斷字串中所有字元是否都屬於可見字元2 name = '\tPuppy'3 result = name.isprintable()4 print(result) #False5 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 string10 print(str_new2) #thwas was string example.....wow!!! thwas is really string
1 #split:字串分割,預設是空格,2 #文法:str.split(str="", num=string.count(str)).3 #str -- 分隔字元,預設為所有的Null 字元,包括空格、換行(\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) #swhte5 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數字(單位元組)