find
与index
都是返回你想寻找的成员的位置string.find(item)
-> item
: 你想查询的元素,返回一个整形
string.index(item)
-> item
: 你想查询的元素,返回一个整形或者报错
Ps:字符串里的位置是从左向右,以0开始的.
find
找不到元素,会返回-1
index
找不到元素,会导致程序报错
# coding:utf-8
info = 'python is a good code'
result = info.find('a')
print(result)
result = info.find('ok')
print(result)
result = info.index('a')
print(result)
result = info.index('o')
print(result)
str1 = "tomorrow is sunny day"
print(str1.find("is", 2, 10))
print(str1.find("is"))
print(str1.find("is", 10, 15))
print(str1.find("is", 9))# coding:utf-8
info = 'python is a good code'
result = info.find('a')
print(result)
result = info.find('ok')
print(result)
result = info.index('a')
print(result)
result = info.index('ok')
print(result)