python練習題自己實現一個字串的find函數

來源:互聯網
上載者:User

標籤:一個   python練習   print   屬性   def   出現   ret   dex   turn   

# 第五題:自己實現一個字串的find函數
# 1.在一個字串中尋找另一個字串
# 2.找到了返回第一次出現的位置
# 3.沒找到返回-1
# 4.參數s1為源字串,參數s2為要尋找的字串
def index_of_str(s1, s2):
#split這個 函數分割字元,返回的 是分割後的元素組成的列表
lt=s1.split(s2)
#判斷分割後的列表的元素個數,如果元素個數為一個,說明列表中只有一個元素,證明s1中沒有s2的字元
if len(lt)==1 :
return -1
#多於一個元素,證明是分割了。用index函數求出s2元素在列表中的下標
else:
return s1.index(s2)
s1="12345678"
s2="45"
index_of_str(s1,s2)
print(index_of_str(s1,s2))

#知識點梳理,主要練習split(),index(),屬性函數的運用,內建函數len()的熟練程度

python練習題自己實現一個字串的find函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.