python中常見字串方法推薦

來源:互聯網
上載者:User
本文主要和大家分享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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.