標籤:字元 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 字串操作函數