溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中的字符串相關(guān)操作說(shuō)明

發(fā)布時(shí)間:2021-09-04 11:46:44 來(lái)源:億速云 閱讀:134 作者:chen 欄目:編程語(yǔ)言

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

(1)切片操作:

str1="hello world!" 

str1[1:3] <=> 'el'(左閉右開(kāi):即是從1到2)

str[:3] <=> 'hel'

str[2:] <=> 'llo world!'

(2)和Java中的字符串一樣,不能直接改變字符串的值,更新字符串時(shí)候可以用切片技術(shù):

str1="hello world!" 

str1=str1[:1]+'python'+str1[1:] <=> 'hpythonello world!'

(3)capitalize():將字符串第一個(gè)字符大寫(xiě)

>>> str='hello world!'
>>> str.capitalize ()
'Hello world!'
>>>

(4)casefold():將整個(gè)字符串小寫(xiě)

>>> str1="Hello world!"
>>> str1.casefold ()
'hello world!'
>>>

(5)center(width):將整個(gè)字符串居中(如果不夠width則用空格補(bǔ)充)

str1="Hello world!"

>>> str1.center(20)
' Hello world! '
>>>

(6)count(sub[,start[,end]]):sub從start到end出現(xiàn)的次數(shù)(默認(rèn)是整個(gè)字符串)

str1="Hello world!"

>>> str1.count ('l',3)
2("Hello world!")
>>> str1.count ('l')
3("Hello world!")
>>> str1.count('l',3,6)
1("Hello world!")
>>>

(7)endswith(sub)判斷是否是以哪個(gè)字符串結(jié)尾

str1="Hello world!"

>>> str1.endswith('orld!')
True("Hello world!")
>>>

(8)expandstabs():將字符串中的'\t'轉(zhuǎn)換為空格

>>> str2='include world!'
>>> str2.expandtabs()
'include world!'
>>>

(9)find(sub[,start][,end]):查找字符串中子串從start到end出現(xiàn)的位置并返回下標(biāo)

str1="Hello world!"

>>> str1.find('llo')
2("Hello world!")
>>> str1.find('llo',3,8)
-1
>>>

(10)isalnum():判斷s是否是數(shù)字或者字母

str1="Hello world!"

>>> str1.isalnum()
False("Hello world!")
>>>

(11)isspace():判斷是否是空格

>>> str=" "
>>> str.isspace()
True
>>>

(12)isdigit():判斷是否都是數(shù)字組成

>>> str="12345dfgbhn"
>>> str.isdigit()
False("12345dfgbhn")
>>>

(13)isalpha():判斷是否都是由字母組成的

>>> str='asdfghj'
>>> str.isalpha()
True
>>>

(14)islower():判斷是否都是由小寫(xiě)字母組成的

>>> str='asdfghj'
>>> str.islower()
True
>>>

(15)istitle():判斷是否是標(biāo)題形式字符串(即是連續(xù)字符串只有第一個(gè)字母大寫(xiě),其他都是小寫(xiě),若是有空格,則每個(gè)分隔的字符串都滿足此)

>>> str='Helloworld'
>>> str.istitle()
True
>>>

(16)isupper():判斷是否都是由大寫(xiě)字母組成的

>>> str='HELLO WOLD'
>>> str.isupper()
True
>>>

(17)join(sub)

>>> str1="abc"
>>> str1.join('1234')
'1abc2abc3abc4'
>>>

(18)lstrip():去掉字符串左邊所有空格

>>> str=" hello world!"
>>> str.lstrip()
'hello world!'
>>>

(19)rstrip():去掉字符串右邊的空格

>>> str="hello world! "
>>> str.rstrip()
'hello world!'
>>>

(20)replace(old,[,new][,count]):將字符串中的old子串替換為new,替換count次

str='hello world!'

>>> str.replace('hello' ,'HELLO' ,2)
'HELLO world! '
>>>

(21)rfind(sub[,start][,end]):從右邊開(kāi)始查找字符串中子串從start到end出現(xiàn)的位置并返回下標(biāo)(注意start和end是從左往右的,返回的也是從左到右的位置。)

>>> str="hello world!"
>>> str.rfind('d!',0,5)
-1
>>> str.rfind('d!')
10
>>>

(22)split(sep):將字符串用給定的標(biāo)準(zhǔn)分割,并且以列表形式返回分割后的元素組

>>> str="1,2,3,4"
>>> str.split(',')
['1', '2', '3', '4']
>>>

(23)startwith(sub[,start][,end]):判斷從start到end是否以sub開(kāi)頭

>>> str.startswith('hel')
True
>>>

(24)strip():去掉字符串左右兩邊的空格

>>> str=' hello world! '
>>> str.strip()
'hello world!'
>>>

(25)swapcase():將字符串的大小寫(xiě)反轉(zhuǎn)

>>> str="Hello world!"
>>> str.swapcase ()
'hELLO WORLD!'
>>>

(26)title()將字符串標(biāo)題化(即是連續(xù)字符串的第一個(gè)字母大寫(xiě),其他都是小寫(xiě)空格,分隔的字符串都遵循此規(guī)則)

>>> str="hello world!"
>>> str.title()
'Hello World!'
>>>

(27)translate(table)

>>> str="sssaabb"
>>> str.translate(str.maketrans('s','b'))
'bbbaabb'
>>>

(28)upper():將整個(gè)字符串都大寫(xiě)

>>> str="hello world!"
>>> str.upper()
'HELLO WORLD!'
>>>

(29)zfill(width):用'0'來(lái)填充不夠的空格(是從左邊開(kāi)始填充)

>>> str="hello world! "
>>> str.zfill(20)
'00000hello world! '
>>>

(30)lower():將整個(gè)字符串都小寫(xiě)

>>> str="HELLO worldQ"
>>> str.lower()
'hello worldq'
>>>

(31)format()

>>> '{0} love {1}{2}'.format('I','my','home')
'I love myhome'
>>> '{0} love {1} {2}'.format('I','my','home')
'I love my home'
>>> '{a} love  {c}'.format(a='I',b='my',c='home')
'I love my home'

>>> '{0:.1f}{1}'.format(27.658,'GB')
'27.7GB'
>>>

(32)格式化:

>>> "%d+%d=%d" % (4,5,4+5)
'4+5=9'
>>>

>>> '%c' % 97
'a'
>>>

“Python中的字符串相關(guān)操作說(shuō)明”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI