当前位置: 首页 > 图灵资讯 > 行业资讯> python中index是什么意思

python中index是什么意思

发布时间:2025-11-13 16:17:29

banner47(2).png

Python index()检测字符串中是否包含子字符串str ,如果指定beg(开始)和end(结束)范围,则检查该方法是否包含在指定范围内,以及python find()方法相同,但如果str不在string中报告异常。

语法

index()方法语法:

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

参数

·str -- 指定检索的字符串。

·beg -- 开始索引,默认为0。

·end -- 结束索引,默认为字符串的长度。

返回值

若包含子字符串返回原始索引值,则抛出异常。

实例

以下实例展示了index()方法的实例:

#!/usr/bin/python

str1="thisisstringexample...wow!!!";
str2="exam";

printstr1.index(str2);
printstr1.index(str2,10);
printstr1.index(str2,40);

上述实例的输出结果如下:

15
15
Traceback(mostrecentcalllast):
File"test.py",line8,in
printstr1.index(str2,40);
ValueError:substringnotfound
shellreturned1

python培训视频众多,全部在python学习网,欢迎在线学习!

(推荐操作系统:windows7系统Python 3.9.1,DELL G3电脑。)

相关文章

python中如何读取excel某列数据?

python中如何读取excel某列数据?

2025-11-14
如何使用Python脚本完成视频播放?

如何使用Python脚本完成视频播放?

2025-11-14
python中vars函数是什么意思?

python中vars函数是什么意思?

2025-11-14
python中str和bytes如何相互转化?

python中str和bytes如何相互转化?

2025-11-14
python中如何使用break跳出for循环?

python中如何使用break跳出for循环?

2025-11-14
python中map函数和filter函数有什么区别?

python中map函数和filter函数有什么区别?

2025-11-14