本文主要和大家分享python中常見字串方法推薦,主要以文字的方式和大家分享,希望能協助到大家。
字串.isalnum() 所有字元都是數字或者字母,為真返回 Ture,否則返回 False。
字串.isalpha() 所有字元都是字母,為真返回 Ture,否則返回 False。
字串.isdigit() 所有字元都是數字,為真返回 Ture,否則返回 False。
字串.islower() 所有字元都是小寫,為真返回 Ture,否則返回 False。
字串.isupper() 所有字元都是大寫,為真返回 Ture,否則返回 False。
字串.istitle() 所有單詞都是首字母大寫,為真返回 Ture,否則返回 False。
字串.isspace() 所有字元都是空白字元,為真返回 Ture,否則返回 False。
print('asdA'.isalpha()) # 純字母print('123'.isdecimal()) # 純數字print('1.1'.isdigit()) # 是否為整數print('ZX'.isupper()) # 是否為大寫
a = ' 字 符 \n 創 'c= a.strip()#預設去掉字串兩邊的空格和分行符號\nc=a.lstrip()#預設去掉左邊的空格c=a.rstrip()#預設去掉右邊的空格print('c....',c)print('a....',a)words = 'https: day is wonderfullday'print(words.strip('day'))#如果strip方法指定一個值的話,那麼會去掉這個指定的值print(words.count('a'))#統計字串出現次數# print(words.index('z'))#找下標,如果找不到會報錯print(words.find('z'))#找下標,如果找不到元素的話,會返回-1print(words.replace('day','DAY')) #替換字串print(words.isdigit()) # 判斷字串是否為純數字print(words.startswith('https:'))#判斷字串是否以xx開頭words.endswith('DAY')#判斷是否以某個字串結尾print(words.upper()) #變成大寫print(words.lower()) #變成小寫username ='adbdsaf33333'print(username.isalpha())#判斷字串是否全為字母print(username.isalnum())#判斷是否包含字母和數字,它是由字母或數字就返回True