Python之字符串-創(chuàng)新互聯(lián)

在Python中字符串是不可變變量,對(duì)其進(jìn)行切片及其中的元素復(fù)制都會(huì)報(bào)錯(cuò)

我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、華容ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的華容網(wǎng)站制作公司>>>?str?=?"This?is?Python" >>>?str[0:3]?=?"abc" Traceback?(most?recent?call?last): ??File?"<stdin>",?line?1,?in?<module> TypeError:?'str'?object?does?not?support?item?assignment

其常用的方法:

  • split

用法:str.split(sep=None, maxsplit=-1)

將字符串劃分為序列

>>>?env?=?"/usr/bin/python" >>>?tmp_env?=?env.split("/") >>>?tmp_env ['',?'usr',?'bin',?'python']

  • replace

用法:str.replace(old,new[,max])

將指定子串替換為另一個(gè)子串,并返回替換后的結(jié)果,但不會(huì)改變?cè)璖tring的內(nèi)容

>>>?str?=?"This?are?Python" >>>?rep_str?=?str.replace("are","is") >>>?rep_str 'This?is?Python' >>>?str 'This?are?Python' >>>?str?=?"This?is?Python,That?is?Great" >>>?rep_str?=?str.replace("is","was",2) >>>?rep_str 'Thwas?was?Python,That?is?Great'
  • find

用法:str.find(sub[, start[, end]])

在字符串中查找字串,若找到,則返回字串的第一個(gè)字符的索引,否則,返回-1

>>>?str?=?"This?is?Python,Hello?,中國(guó),您好" >>>?fd_str?=?str.find("is") >>>?fd_str 2 >>>?str[fd_str] 'i' >>>?fd_str2?=?str.find("中") >>>?str[fd_str2] '中' >>>?str1?=?"That?is?dog".find("G") >>>?str1 -1
  • join

用法:str.join(iterable)

合并序列的元素

>>>?dirs?=?['','usr','bin','python'] >>>?'/'.join(dirs) '/usr/bin/python' >>>?num?=?[1,2,3,4]???#合并數(shù)字列表,報(bào)錯(cuò) >>>?seq?=?'+' >>>?seq.join(num) Traceback?(most?recent?call?last): ??File?"<stdin>",?line?1,?in?<module> TypeError:?sequence?item?0:?expected?str?instance,?int?found >>>?str1?=?["A","B","C"]??#合并字符列表,OK >>>?seq?=?"-" >>>?seq.join(str1) 'A-B-C' >>>?str1?=?"ABCDE" >>>?seq?=?"abcde" >>>?seq.join(str1) 'AabcdeBabcdeCabcdeDabcdeE'
  • strip

用法:str.strip([chars])

將字符串開(kāi)頭和末尾的空白(不包括中間的空白)刪除,并返回結(jié)果

>>>?"??Hello,World!".strip() 'Hello,World!' >>>?"Hello,World????".strip() 'Hello,World' >>>?"??Hello,World???".strip() 'Hello,World' >>>?"Hello,???World!?!".strip() 'Hello,???World!?!' >>>?"www.baidu.com".strip("com") 'www.baidu.' >>>?"www.baidu.com".strip(".com") 'www.baidu'

  • center

用法:str.center(width[, fillchar])

通過(guò)在兩邊填充字符(默認(rèn)為空格)讓string居中

>>>?"0123456789".center(1) '0123456789' >>>?"0123456789".center(-1) '0123456789' >>>?"0123456789".center(10) '0123456789' >>>?"0123456789".center(11) '?0123456789' >>>?"0123456789".center(11,"*") '*0123456789' >>>?"0123456789".center(12,"*") '*0123456789*' >>>?"0123456789".center(20,"*") '*****0123456789*****'

詳細(xì)的String參數(shù)可參考官網(wǎng)文檔:

https://docs.python.org/3.7/library/stdtypes.html?highlight=replace#str.replace

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前文章:Python之字符串-創(chuàng)新互聯(lián)
URL鏈接:http://bm7419.com/article8/diceop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站改版、響應(yīng)式網(wǎng)站、企業(yè)網(wǎng)站制作做網(wǎng)站、搜索引擎優(yōu)化

廣告

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

外貿(mào)網(wǎng)站制作