Python参考手册
Python find()方法
Python find()方法
Python find()方法
Python 字符串描述
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
语法
find()方法语法:
str.find(str, beg=0, end=len(string))
参数
- str -- 指定检索的字符串
- beg -- 开始索引,默认为0。
- end -- 结束索引,默认为字符串的长度。
返回值
如果包含子字符串返回开始的索引值,否则返回-1。
实例
以下实例展示了find()方法的实例:
实例(Python 2.0+)
#!/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 2.0+)
>>>info = 'abca'
>>> print info.find('a') # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
0
>>> print info.find('a',1) # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3
3
>>> print info.find('3') # 查找不到返回-1
-1
>>>
Python 字符串
Python find()方法 |
---|
Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人 Guido van Rossum 于1989年发明,第一个公开发行版发行于1991年。 Python 是纯粹的自由软件, 源代码和解释器 CPython 遵循 GPL 协议。Python 语法简洁清晰,特色之一是强制用空白符( white space )作为语句缩进。
主页 | https://www.python.org/ |
源码 | https://github.com/python/cpython |
版本 | 2.7 |
发布版本 | 2.7.13 |