Fork me on GitHub

概念:

  Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

语法

find()方法语法:

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

参数

  • str -- 指定检索的字符串
  • beg -- 开始索引,默认为0。
  • end -- 结束索引,默认为字符串的长度。

返回值

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

实例

下面的实例可以基本覆盖find的用法:

 1 #!/usr/bin/python
 2 
 3 str1 = "this is string example....wow!!!";
 4 str2 = "exam";
 5 
 6 print str1.find(str2);
 7 print str1.find(str2, 10);
 8 print str1.find(str2, 40);
 9 
10 info = 'abca'
11 print info.find('a')
12 print info.find('a',1)
13 print info.find('3')
14 print info.find('1')
15 print info.find('c')
16 ~                         

 运行结果:

15
15
-1
0
3
-1
-1
2

  

posted on 2018-11-21 14:31  虚生  阅读(19174)  评论(0编辑  收藏  举报