教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python中index是什么意思

python中index是什么意思

发布时间:2021-03-26   编辑:jiaochengji.com
教程集为您提供python中index是什么意思等资源,欢迎您收藏本站,我们将为您提供最新的python中index是什么意思资源

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 = "this is string example....wow!!!";
str2 = "exam";
 
print str1.index(str2);
print str1.index(str2, 10);
print str1.index(str2, 40);

以上实例输出结果如下:

15
15
Traceback (most recent call last):
  File "test.py", line 8, in 
  print str1.index(str2, 40);
ValueError: substring not found
shell returned 1

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

您可能感兴趣的文章:
python中index是什么意思
python index函数是什么意思
python中的%是什么意思
python中r代表什么意思
python中转义字符是什么意思
python中num是什么意思
python中divmod是什么意思
python中的def语句是什么意思
python中abs是什么意思
string在python中是什么意思

[关闭]
~ ~