您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Python字符串方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
將字符串分割成列表,默認以空格為分隔符
a="you can't see mee" a.split() #輸出內(nèi)容為 ["you","can't","see","me"] a.split(" ' ") #輸出內(nèi)容為['you cant','t see me']
用于移除字符串兩端的字符 ;當括號為空時候,默認刪除空白符(包括'\n', '\r', '\t', ' ')
a=" 123" a.strip() #輸出內(nèi)容為 "123" ,注意前面有a=" 123"前面空格
例子2:
a= "0000000this is string example....wow!!!0000000" print a.strip("0") #以上實例輸出結(jié)果如下: this is string example....wow!!!
特別說明: 只要刪除內(nèi)容存在,不論順序正反都一樣 如strip("12") 和strip("21"),如下所示
c="123acb" c.strip("12") #輸出內(nèi)容為"3abc" c.strip("21") #輸出內(nèi)容一樣為"3abc"
python的strip函數(shù)有兩種用法:一般去首尾
如果省略參數(shù),那么將會執(zhí)行去除兩端空格。如:
str=" abc " print(str.strip()) #結(jié)果為abc
如果傳入了參數(shù),那么將按照字符在兩端去除相應(yīng)字符,但這時候和空格沒有任何關(guān)系。
str=" abc " print(str.strip(" a")) #輸出"bc" print(str.strip("ac")) #輸出" abc " 什么都沒做 print(str.strip("a")) #輸出" abc "什么都沒做
將 字符串,列表,字典,元組中的元素鏈接成新的字符串
a="123" " |".join(a) >>'1 |2 |3' b=['a','b','c'] " ".join(b) >>'a b c' c=('i','j','k') "_".join(c) >>'i_j_k' s = {"name":"lee","age":18} "_".join(s) >>'name_age'
注意:列表,元組等序列里面的內(nèi)容必須是字符串,否則會報錯
字符串替換,第一個參數(shù)舊字符串,第二個要替換的字符串,第三個替換的次數(shù),可為空默認全部替換
s = "hello python python python" print(s.replace("python", "java")) print(s.replace("python", "java",2)) #輸出 hello java java java hello java java python
檢測字符串中是否包含子字符串 str 查找內(nèi)容在第幾個字符,不存在返回**-1**
a="you can't see me" a.find("you") #輸出內(nèi)容為0 afind("can't") #輸出內(nèi)容為4 a.find("asd") #輸出內(nèi)容為-1
檢測字符串中是否包含子字符串 str
用法和find() 差不多,不過如果查找內(nèi)容不存在,返回一個錯誤,如果指定 beg(開始) 和 end(結(jié)束) 范圍,則檢查是否包含在指定范圍內(nèi)
a="you can't see me" a.index("you") #輸出內(nèi)容為0 a.index("can't") #輸出內(nèi)容為4 a.index("asd") # 輸出內(nèi)容為: Traceback (most recent call last): File "<stdin>", line 1,in <module> ValueError: substring not found ``` 如果參數(shù)出現(xiàn)很多次,要如何做呢? 例2: ``` t=tuple('Allen') print(t) #輸出 ('A', 'l', 'l', 'e', 'n') a=t.index('l',2) print(a) #輸出2 ``` 因為第一個’l’的出現(xiàn)位置是1,所以我們將開始索引加1繼續(xù)尋找,果然,在索引為2的位置又找到了’l’。
seek()函數(shù)是屬于文件操作中的函數(shù),用來移動文件讀取指針到指定位置。
語法:
fileObject.seek(offset[, whence]) #offset – 開始的偏移量,也就是代表需要移動偏移的字節(jié)數(shù) #whence:可選,默認值為 0。給offset參數(shù)一個定義,表示要從哪個位置開始偏移;0代表從文件開頭開始 #算起,1代表從當前位置開始算起,2代表從文件末尾算起。
轉(zhuǎn)換成大寫
s='abc' s.upper() #輸出ABC
轉(zhuǎn)換成小寫
大寫字母轉(zhuǎn)換成小寫,小寫字母轉(zhuǎn)換成大寫
把字符串中,第一個字符轉(zhuǎn)換成大寫,其余轉(zhuǎn)換成小寫
把字符串中,每個單詞的首字母改成大寫
注意:以上方法不改變原來的字符串,產(chǎn)生一個新的字符串
案列:
s= "heLlO World" a=s.swapcase() b=s.capitalize() c=s.title() d = s.upper() e = s.lower() print("swapcase:",a) print("capitalize:",b) print("title:",c) print("upper:",d) print("lower:",e) print(s.isalpha()) print(s) #輸出 swapcase: HElLo wORLD capitalize: Hello world title: Hello World upper: HELLO WORLD lower: hello world False #注意有空格不算字母 heLlO World
是否全部為字母,注意:有空格就不算字母了,中文字符串算字母
s= "heLlO World" print(s.isalpha()) s1 = "hello" print(s1.isalpha()) print("張三".isalpha()) print("張三1".isalpha()) #輸出 False True True False
是否全部由數(shù)字組成
注意:中文數(shù)字,羅馬數(shù)字,字符串數(shù)字,還有輸入法中的算做數(shù)字的特殊符號都是算是數(shù)字。
英文數(shù)字不算
print("1234".isnumeric()) print("一二34".isnumeric()) print("一二".isnumeric()) print("one".isnumeric()) #英文不是 print("ⅠⅡⅢⅣ".isnumeric()) print("㈠".isnumeric()) #輸出 True True True False True True
特殊數(shù)字符號
print("?".isnumeric()) print("①".isnumeric()) print("⒈".isnumeric()) #注意這里不是一點,輸入法特殊符號中數(shù)字 print("⒒".isnumeric()) print("⑴".isnumeric()) print("⑾".isnumeric()) True True True True True True
是否全部由字母數(shù)字組成
注意:輸入法中的特殊符號算數(shù)字,常見標點符號不算字母數(shù)字如 !等
“Python字符串方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。