07-Python-字串操作

來源:互聯網
上載者:User

標籤:dex   format   nbsp   sde   列印   lower   style   結果   標識   

1、常用字串操作

 1 name = ‘my name is druid‘ 2  3 print(name.capitalize())  #首字母大寫 4 print(name.count(‘d‘)) 5 print(name.center(50,‘-‘))  #一共列印五十個字元,字串置中,不夠的位用-填充 6 print(name.ljust(50,‘*‘))  #一共列印五十個字元,字串置左,不夠的位用*填充 7 print(name.rjust(50,‘~‘))  #一共列印五十個字元,字串置右,不夠的位用~填充 8 print(name.endswith(‘d‘))  #是否以字串‘d‘結尾 9 print(name.expandtabs(tabsize=30))  #tab鍵為30個空格10 print(name.find(‘d‘))  #返回字元的第一個索引,沒有找到返回-111 print(name[name.find(‘d‘):])  #切片12 print(‘druid‘.rfind(‘d‘))    #返回最後一個字元的索引13 print(name.index(‘d‘))  #返回索引,沒有找到則報錯14 print(name[name.index(‘d‘):])  #切片15 16 s = ‘a123A‘17 print(s.isalnum())  #是否是字母數字18 print(s.isalpha())  #是否是純英文字元19 print(s.isdecimal())  #是否是十進位20 print(s.isdigit())  #是否是整數21 print(s.isidentifier())  #是否是合法的標識符(變數名)22 print(s.islower())  #是否是小寫23 print(s.isnumeric())  #是否僅有數字24 print(s.istitle())  #每個單詞開頭是否為大寫25 print(s.isupper())  #是否為大寫26 print(‘‘.join((‘druid!‘)) ) #把可迭代對象用‘‘中的字串串連起來27 print(‘+‘.join([‘a‘,‘b‘,‘c‘]))28 print(‘DruiD‘.lower())  #小寫29 print(‘druid‘.upper())  #大寫30 31 print(‘\n    druid‘.lstrip())  #去掉左邊空格和分行符號32 print(‘druid      \n‘.rstrip())  #去掉右邊空格和分行符號33 print(‘\n  druid   \n‘.strip())  #去掉兩邊的空格和分行符號34 35 p = str.maketrans(‘abcde‘,‘12345‘)  #前面的字串用後面的字串替換36 print(‘abcde‘.translate(p))  #如果前面的字串和p中的字串有相同的字元,則替換37 print(‘druid abcde‘.translate(p))  #前面的字串和p中的字串不相同的字元不替換38 39 print(‘druid‘.replace(‘d‘,‘D‘,1))  #將第一個字元替換,如果不加1,則替換所有40 41 print(‘druid chenc‘.split())  #預設按照空格分隔字元,得到的結果為列表42 print(‘druid chenc‘.split(‘d‘))  #以d為分隔字元43 print(‘druid chenc‘.split(‘c‘))  #以c為分隔字元44 print(‘1+2+3‘.split(‘+‘))  #以+為分隔字元45 print(‘1+2\n+3+4‘.splitlines())  #按分行符號分隔46 47 print(‘druid chen‘.zfill(50))  #左邊用零填充48 49 name1 = ‘my name is {name},age {age}‘50 print(name1.format(name = ‘druid‘,age = 18))51 print(name1.format_map({‘name‘:‘alex‘,‘age‘:33}))

 

07-Python-字串操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.