Python3 find()方法

Python3 字符串 Python3 字符串


描述

find() 方法檢測字符串中是否包含子字符串 str ,如果指定 beg(開始) 和 end(結(jié)束) 范圍,則檢查是否包含在指定范圍內(nèi),如果包含子字符串返回開始的索引值,否則返回-1。

語法

find()方法語法:

str.find(str, beg=0, end=len(string))

參數(shù)

  • str -- 指定檢索的字符串
  • beg -- 開始索引,默認(rèn)為0。
  • end -- 結(jié)束索引,默認(rèn)為字符串的長度。

返回值

如果包含子字符串返回開始的索引值,否則返回-1。

實例

以下實例展示了find()方法的實例:

#!/usr/bin/python3

str1 = "W3CSchool example....wow!!!"
str2 = "exam";

print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 15))

以上實例輸出結(jié)果如下:

10
10
-1

Python3 字符串 Python3 字符串