Python字符串方法

本篇內(nèi)容介紹了“Python字符串方法”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)是專業(yè)的福綿網(wǎng)站建設(shè)公司,福綿接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行福綿網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

格式化字符串

  • split()

將字符串分割成列表,默認(rèn)以空格為分隔符

a="you can't see mee"
a.split()             #輸出內(nèi)容為 ["you","can't","see","me"]
a.split(" ' ")        #輸出內(nèi)容為['you cant','t see me']
  • strip()

用于移除字符串兩端的字符 ;當(dāng)括號為空時(shí)候,默認(rèn)刪除空白符(包括'\n', '\r', '\t', ' ')

a="        123"
a.strip()                        #輸出內(nèi)容為 "123" ,注意前面有a=" 123"前面空格

例子2:

a= "0000000this is string example....wow!!!0000000"
print a.strip("0")

#以上實(shí)例輸出結(jié)果如下:
this is string example....wow!!!

特別說明: 只要?jiǎng)h除內(nèi)容存在,不論順序正反都一樣 如strip("12") 和strip("21"),如下所示

c="123acb"
c.strip("12")                #輸出內(nèi)容為"3abc"
c.strip("21")                #輸出內(nèi)容一樣為"3abc"

strip()總結(jié):

python的strip函數(shù)有兩種用法:一般去首尾

  • 如果省略參數(shù),那么將會(huì)執(zhí)行去除兩端空格。如:

 str="   abc   "  
print(str.strip()) #結(jié)果為abc
  • 如果傳入了參數(shù),那么將按照字符在兩端去除相應(yīng)字符,但這時(shí)候和空格沒有任何關(guān)系。

str="  abc "  
print(str.strip(" a")) #輸出"bc"  
print(str.strip("ac")) #輸出" abc " 什么都沒做  
print(str.strip("a")) #輸出" abc "什么都沒做

join()

將 字符串,列表,字典,元組中的元素鏈接成新的字符串

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)容必須是字符串,否則會(huì)報(bào)錯(cuò)

replace(old, new,替換次數(shù))

字符串替換,第一個(gè)參數(shù)舊字符串,第二個(gè)要替換的字符串,第三個(gè)替換的次數(shù),可為空默認(rèn)全部替換

s = "hello python python python"
print(s.replace("python", "java"))
print(s.replace("python", "java",2))

#輸出
hello java java java
hello java java python

find()

檢測字符串中是否包含子字符串 str 查找內(nèi)容在第幾個(gè)字符,不存在返回**-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

index()

檢測字符串中是否包含子字符串 str
用法和find() 差不多,不過如果查找內(nèi)容不存在,返回一個(gè)錯(cuò)誤,如果指定 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
```
因?yàn)榈谝粋€(gè)’l’的出現(xiàn)位置是1,所以我們將開始索引加1繼續(xù)尋找,果然,在索引為2的位置又找到了’l’。

seek()

seek()函數(shù)是屬于文件操作中的函數(shù),用來移動(dòng)文件讀取指針到指定位置。
語法:

fileObject.seek(offset[, whence])
#offset – 開始的偏移量,也就是代表需要移動(dòng)偏移的字節(jié)數(shù)
#whence:可選,默認(rèn)值為 0。給offset參數(shù)一個(gè)定義,表示要從哪個(gè)位置開始偏移;0代表從文件開頭開始

#算起,1代表從當(dāng)前位置開始算起,2代表從文件末尾算起。

upper()

轉(zhuǎn)換成大寫

s='abc'
s.upper()
#輸出ABC

lower()

轉(zhuǎn)換成小寫

swapcase()

大寫字母轉(zhuǎn)換成小寫,小寫字母轉(zhuǎn)換成大寫

capitalize()

把字符串中,第一個(gè)字符轉(zhuǎn)換成大寫,其余轉(zhuǎn)換成小寫

title()

把字符串中,每個(gè)單詞的首字母改成大寫

注意:以上方法不改變原來的字符串,產(chǎn)生一個(gè)新的字符串

案列:

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

isalpha()

是否全部為字母,注意:有空格就不算字母了,中文字符串算字母

s= "heLlO World"
print(s.isalpha())
s1 = "hello"
print(s1.isalpha())
print("張三".isalpha())
print("張三1".isalpha())

#輸出
False
True
True
False

isnumeric()

是否全部由數(shù)字組成

注意:中文數(shù)字,羅馬數(shù)字,字符串?dāng)?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())  #注意這里不是一點(diǎn),輸入法特殊符號中數(shù)字 
print("⒒".isnumeric())
print("⑴".isnumeric())
print("⑾".isnumeric())

True
True
True
True
True
True

Python字符串方法

isalnum()

是否全部由字母數(shù)字組成

注意:輸入法中的特殊符號算數(shù)字,常見標(biāo)點(diǎn)符號不算字母數(shù)字如 !等

“Python字符串方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章標(biāo)題:Python字符串方法
當(dāng)前路徑:http://muchs.cn/article32/ihjdpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化網(wǎng)站收錄、網(wǎng)站制作網(wǎng)站導(dǎo)航、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)