Python3 find()方法

Python3 字符串 Python3 字符串


描述

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

語(yǔ)法

find()方法語(yǔ)法:

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

參數(shù)

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

返回值

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

實(shí)例

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

#!/usr/bin/python3

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

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

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

10
10
-1

Python3 字符串 Python3 字符串