Latest web development tutorials

Python find()方法

Python 字符串 Python字符串


描述

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

語法

find()方法語法:

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

參數

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

返回值

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

實例

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

#!/usr/bin/python

str1 = "this is string example....wow!!!";
str2 = "exam";

print str1.find(str2);
print str1.find(str2, 10);
print str1.find(str2, 40);

以上實例輸出結果如下:

15
15
-1

Python 字符串 Python字符串