python list的index()和find()的實現
index()
Python index() 方法檢測字符串中是否包含子字符串 str ,如果指定 beg(開始) 和 end(結束) 范圍,則檢查是否包含在指定范圍內,該方法與 python find()方法一樣,只不過如果str不在 string中會報一個異常。
語法
index()方法語法:
str.index(str, beg=0, end=len(string))
參數
str ? 指定檢索的字符串 beg ? 開始索引,默認為0。 end ? 結束索引,默認為字符串的長度。返回值
如果包含子字符串返回開始的索引值,否則拋出異常。
實例
>>> str1 = 'This is a example'>>> str2 = 'exam'>>>>>> str1.index(str2)10>>> str1.index(str2,5)10>>> str1.index(str2,11)Traceback (most recent call last): File '<stdin>', line 1, in <module>ValueError: substring not found>>> str1.index(str2,5,11)Traceback (most recent call last): File '<stdin>', line 1, in <module>ValueError: substring not found>>>
find()
Python find() 方法檢測字符串中是否包含子字符串 str ,如果指定 beg(開始) 和 end(結束) 范圍,則檢查是否包含在指定范圍內,如果包含子字符串返回開始的索引值,否則返回-1。
語法
str.find(str, beg=0, end=len(string))
參數
str ? 指定檢索的字符串 beg ? 開始索引,默認為0。 end ? 結束索引,默認為字符串的長度。返回值
如果包含子字符串返回開始的索引值,否則返回-1。
實例
>>> str1 = 'This is a example'>>> str2 = 'exam'>>>>>> str1.find(str2)10>>> str1.find(str2,5)10>>> str1.find(str2,11)-1>>> str1.find(str2,5,11)-1>>>
到此這篇關于python list的index()和find()的實現的文章就介紹到這了,更多相關python list的index()和find()內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. js select支持手動輸入功能實現代碼2. PHP正則表達式函數preg_replace用法實例分析3. vue使用moment如何將時間戳轉為標準日期時間格式4. Android studio 解決logcat無過濾工具欄的操作5. vue-drag-chart 拖動/縮放圖表組件的實例代碼6. 什么是Python變量作用域7. Android 實現徹底退出自己APP 并殺掉所有相關的進程8. bootstrap select2 動態從后臺Ajax動態獲取數據的代碼9. Android Studio3.6.+ 插件搜索不到終極解決方案(圖文詳解)10. 一個 2 年 Android 開發者的 18 條忠告
