Python 字串操作函數

來源:互聯網
上載者:User

標籤:字元   word   split   not found   ali   tar   英文   lower   文字   

#-*- coding:utf-8 -*-strword = "i will fly with you , fly on the sky ."#findprint(strword.find("fly"))   #列印7#find返回找到第一個字串的下標,找不到返回-1print("==rfind==")#rfindprint(strword.rfind("fly"))#rfind 從後向前尋找,找到返回下標,找不到返回-1print("==index==")#indexprint(strword.index("fly")) #index 找到返回首字元的下標,找不到報錯ValueError: substring not foundprint("==rindex==")#rindexprint(strword.rindex("fly"))#功能與rfind類似,傳回值不同print("==count==")#countprint(strword.count("fly"))#count 返回找到字串的個數,找不到返回0print("==replace==")#replaceprint(strword.replace("fly","xxx"))#全文替換指定字串,由於字串屬於不可變類型,所以replace()函數不會修改strword的值#replace擴充 可以指定替換多少個,但是替換的順序還是從前向後的print(strword.replace("fly","xxx",1))print("===split==")#splitprint(strword.split(" "))#split 切割函數,按照指定字元切割字串word = "hello world"print("==capitalize==")#capitalizeprint(word.capitalize())#字串首單詞的第一個字元大寫print("==title==")print(word.title())#title 字串中每個單字首大寫print("==startswith==")print(word.startswith("he"))#startswith 是否以某字串開頭,是返回true,不是返回flaseprint("==endswith==")print(word.endswith("ld"))#endswith 判斷以某字串結尾,是返回true,不是返回flasenword = "A B C a b c"print("==lower==")print(nword.lower())#lower 將所有英文字元轉化成小寫print("==upper==")#upperprint(nword.upper())#upper 將所有的英文字元轉化成大寫

 

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.