Latest web development tutorials

Python3 find()方法

Python3 字符串 Python3字符串


描述

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

語法

find()方法語法:

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

參數

  • str -- 指定檢索的字符串
  • beg -- 開始索引,默認為0。
  • end -- 結束索引,默認為字符串的長度。

返回值

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

實例

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

#!/usr/bin/python3

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

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

以上實例輸出結果如下:

7
7
-1

Python3 字符串 Python3字符串