教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python中的find函数怎么用

python中的find函数怎么用

发布时间:2021-04-11   编辑:jiaochengji.com
教程集为您提供python中的find函数怎么用等资源,欢迎您收藏本站,我们将为您提供最新的python中的find函数怎么用资源

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

语法

find()方法语法:

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

相关推荐:《Python视频教程》

参数

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);

info = 'abca'
print info.find('a')
print info.find('a',1)
print info.find('3')
print info.find('1')
print info.find('c')

运行结果:

15
15
-1
0
3
-1
-1
2

您可能感兴趣的文章:
python中的find函数怎么用
php str_ireplace函数怎么用
python中怎么保留小数
python 重载内置函数吗
python lambda怎么用
python怎么生成随机不重复数组
python中怎么删除列表中的空元素
python怎么分读txt中逗号
python输入错误怎么删除
python怎么才能使随机整数不重复

[关闭]
~ ~