<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          盤點(diǎn)Python字符串常見的16種操作方法

          共 3018字,需瀏覽 7分鐘

           ·

          2021-07-11 09:12

          點(diǎn)擊上方“Go語言進(jìn)階學(xué)習(xí)”,進(jìn)行關(guān)注

          回復(fù)“Go語言”即可獲贈(zèng)從入門到進(jìn)階共10本電子書

          桃李春風(fēng)一杯酒,江湖夜雨十年燈。

          大家好,我是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。

          ------------------- End -------------------

          往期精彩文章推薦:

          歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

          想加入Go學(xué)習(xí)群請?jiān)诤笈_(tái)回復(fù)【入群

          萬水千山總是情,點(diǎn)個(gè)【在看】行不行

          瀏覽 45
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  午夜福利樱桃网 | 91视频最新网址 | 全部免费毛片在线播放网站 | 日韩无码小电影 | 成人AV性爱 |