str字符串 find( ) 方法

描述

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

语法

find()方法语法:spa

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

 

参数

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

返回值

若是包含子字符串返回开始的索引值,不然返回-1。code

实例

如下实例展现了find()方法的实例:blog

#!/usr/bin/python3
 
str1 = "Runoob example....wow!!!"
str2 = "exam";
 
print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 10))

 

以上实例输出结果以下:索引

7
7
-1

实例字符串

>>>info = 'abca'
>>> print(info.find('a'))      # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
0
>>> print(info.find('a', 1))   # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3
3
>>> print(info.find('3'))      # 查找不到返回-1
-1
>>>
相关文章
相关标签/搜索