盤點(diǎn)Python字符串常見的16種操作方法
大家好,我是Go進(jìn)階者,上篇文章給大家介紹了Python字符串,今天給大家分享一些Python字符串的常用操作,一起來看看吧~
一、常用操作
以字符串
- 'lstr = 'welcome to Beijing Museumitcpps fdsfs'
為例,介紹字符常見的操作。
<1> find
檢測 str 是否包含在 lstr中,如果是返回開始的索引值,否則返回-1。
語法:
- lstr.find(str, start=0, end=len(lstr))
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.find("Museum"))
- print(lstr.find("dada"))
運(yùn)行結(jié)果:
<2> index
跟find()方法一樣,只不過如果str不在 lstr中會(huì)報(bào)一個(gè)異常。
語法:
- lstr.index(str, start=0, end=len(lstr))
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.index("dada"))
運(yùn)行結(jié)果:
<3> count
返回 str在start和end之間 在 lstr里面出現(xiàn)的次數(shù)
語法:
- lstr.count(str, start=0, end=len(lstr))
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.count("s"))
運(yùn)行結(jié)果:
<4> replace
把 lstr 中的 str1 替換成 str2,如果 count 指定,則替換不超過 count 次.
- 1str.replace(str1, str2, 1str.count(str1))
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.replace("s", "ttennd"))
運(yùn)行結(jié)果:
<5> split
以 str 為分隔符切片 lstr,如果 maxsplit有指定值,則僅分隔 maxsplit 個(gè)子字符串
- 1str.split(str=" ", 2)
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.split("to", 5))
運(yùn)行結(jié)果:
<6> capitalize
把字符串的第一個(gè)字符大寫。
- lstr.capitalize()
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.capitalize())
運(yùn)行結(jié)果:
<7> title
把字符串的每個(gè)單詞首字母大寫。
- >>> a = "hello itcast"
- >>> a.title()
- 'Hello Itcast' #運(yùn)行結(jié)果
<8> startswith
檢查字符串是否是以 obj 開頭, 是則返回 True,否則返回 False
- 1str.startswith(obj)
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.startswith('we'))
運(yùn)行結(jié)果:
<9> endswith
檢查字符串是否以obj結(jié)束,如果是返回True,否則返回 False.
- 1str.endswith(obj)
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.endswith('hfs'))
運(yùn)行結(jié)果:
<10> lower
轉(zhuǎn)換 lstr 中所有大寫字符為小寫
- 1str.lower()
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.lower())
運(yùn)行結(jié)果:
<11> upper
轉(zhuǎn)換 lstr 中的小寫字母為大寫
- 1str.upper()
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.upper())
運(yùn)行結(jié)果:
<12> strip
刪除lstr字符串兩端的空白字符。
- >>> a = "\n\t itcast \t\n"
- >>> a.strip()
- 'itcast' #運(yùn)行結(jié)果
<13> rfind
類似于 find()函數(shù),不過是從右邊開始查找。
- 1str.rfind(str, start=0,end=len(1str) )
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.rfind('eijing'))
運(yùn)行結(jié)果:
<14> rindex
類似于 index(),不過是從右邊開始。
- 1str.rindex( str, start=0,end=len(1str))
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.rindex('eijing'))
運(yùn)行結(jié)果:
<15> partition
把lstr以str分割成三部分,str前,str和str后。
- 1str.partition(str)
例:
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- print(lstr.partition('eijing'))
運(yùn)行結(jié)果:
<16> join
mystr 中每個(gè)字符后面插入str,構(gòu)造出一個(gè)新的字符串。
- lstr = 'welcome to Beijing Museumitcpps fdsfs'
- str='233'
- lstr.join(str)
- li=["my","name","is","LY"]
- print(str.join(li))
運(yùn)行結(jié)果:
二、總結(jié)
本文詳細(xì)的講解了Python基礎(chǔ) ( 字符串 )。介紹了有關(guān)字符串,切片的操作。下標(biāo)索引。以及在實(shí)際操作中會(huì)遇到的問題,提供了解決方案。希望可以幫助你更好的學(xué)習(xí)Python。