python中find函数是什么意思 python的find函数什么意思

主机教程 建站分享 2年前 (2022-10-16) 162次浏览

文章摘要:python中find函数是什么意思 python的find函数什么意思

python中find函数是实现检索字符串并且输出运算值的意思,语法格式为:“str.find(str, be […]

python中find函数是实现检索字符串并且输出运算值的意思,语法格式为:“str.find(str, beg=0, end=len(string))”,这里“str”代表指定检索的字符串、“beg”代表开始索引,默认为0、“end”代表结束索引,默认为字符串的长度。

具体使用步骤:

1、首先打开python编辑器,新建一个python项目。

2、在python项目中定义2个字符串。

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

str2 = "exam";

3、再使用find函数检测字符串str1中是否包含子字符串str2并返回索引值。

print str1.find(str2);

返回结果:

15

相关示例代码:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"

print("The position of Best site is at:", mystring.find("Best site", 5, 40))

print("The position of Guru99 is at:", mystring.find("Guru99", 20))

#输出结果

The position of Best site is at: 27

The position of Guru99 is at: -1


声明:
若非注明,本站文章源于互联网收集整理和网友分享发布,如有侵权,请联系站长处理。
文章名称:python中find函数是什么意思 python的find函数什么意思
文章链接:https://www.7966.org/post/10597.html
转载请注明出处

喜欢 (0)