Python string常用函數

來源:互聯網
上載者:User

標籤:count   print   new   ace   start   常用   nbsp   搜尋   tar   

2017-07-03 23:26:08

1、.replace(self, old, new, count=-1)

replace()函數將舊字串替換為新字串,最後一個參數count為可選項,表示替換最多count次(小於count)。

注意這種替換返回替換後的字串,源字串是不改變的。

s=‘ABCDEF‘out=s.replace(‘EF‘,‘ef‘)print(s)print(out)輸出:ABCDEFABCDef

 

2、.find(self, sub, start=0, end=len)

find()函數返回第一次字元數sub的下標索引,預設情況下是掃描整個字串,不過可以自行設定start,end,和stl一致的是,這裡的起始區間前閉後開。

沒有找到的,返回-1

s=‘ABCDEF‘print(s.find(‘BC‘,0,1))print(s.find(‘CD‘))輸出:-12

.rfind()函數則是從右向左進行搜尋,即返回索引值最大的sub字串的下標,若沒有搜尋到,則返回-1。

3、

 

Python string常用函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.