day39
---------------------------------------------------------------
实例061:查找字符串
题目 查找字符串。
分析:查找字符串python提供了4种方法:其中find和index方法,是从左到右开始查找,加r代表从右到左查找;find方法返回找到后第一个子串的下标,未找到返回-1,而index方法未找到会抛出异常。
代码如下:
s1 = "qinglaoshi"
print(s1.find("ao"))
print(s1.find("i"))
print(s1.rfind("i"))
print(s1.index("i"))
print(s1.rindex("i"))
print("----", s1.index("as"))
结果如下:
C:\Users\qgc\AppData\Local\Programs\Python\Python39\python.exe D:/pythonproject/aaa.py
Traceback (most recent call last):
File "D:\pythonproject\aaa.py", line 7, in <module>
print("----", s1.index("as"))
ValueError: substring not found
5
1
9
1
9
Process finished with exit code 1