温馨提示×

python中find函数是什么意思

摘星
5846
2021-05-31 16:49:47
栏目: 编程语言

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

python中find函数是什么意思

具体使用步骤:

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

0