Python中的字符串相關操作說明-創(chuàng)新互聯

本篇內容介紹了“Python中的字符串相關操作說明”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

創(chuàng)新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網站建設、成都做網站、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的蓬江網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!

(1)切片操作:

str1="hello world!" 

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

str[:3] <=> 'hel'

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

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

str1="hello world!" 

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

(3)capitalize():將字符串第一個字符大寫

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

(4)casefold():將整個字符串小寫

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

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

str1="Hello world!"

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

(6)count(sub[,start[,end]]):sub從start到end出現的次數(默認是整個字符串)

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)判斷是否是以哪個字符串結尾

str1="Hello world!"

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

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

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

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

str1="Hello world!"

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

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

str1="Hello world!"

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

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

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

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

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

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

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

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

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

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

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

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

>>> 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]):從右邊開始查找字符串中子串從start到end出現的位置并返回下標(注意start和end是從左往右的,返回的也是從左到右的位置。)

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

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

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

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

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

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

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

(25)swapcase():將字符串的大小寫反轉

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

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

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

(27)translate(table)

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

(28)upper():將整個字符串都大寫

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

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

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

(30)lower():將整個字符串都小寫

>>> 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中的字符串相關操作說明”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯-成都網站建設公司網站,小編將為大家輸出更多高質量的實用文章!

網頁題目:Python中的字符串相關操作說明-創(chuàng)新互聯
當前網址:http://bm7419.com/article12/ddjjdc.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站維護網站收錄自適應網站、ChatGPT網站制作、網站設計

廣告

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

綿陽服務器托管