>>> string = "Winter has come, can spring be far behind?"
>>> string.endswith('?')
True
>>> string.endswith("far behind?")
True
>>> string.endswith("behind")
False
>>> string.endswith("come", 11, 15)
True
>>> string.endswith("e", 11, 15)
True
>>>
startswith(prefix[, start[, end]]) 见第三弹
find(sub[, start[, end]])
释义
寻找字符串中是否包含 sub 子字符串
若有,返回索引值;若无,返回 -1
>>> string = "When I was a child, happiness is a very simple thing."
>>> string.find("is")
30
>>> string.find("is", 40)
-1
>>> string.find("simple", 30, 50)
40
>>>
rfind(sub[, start[, end]]) 用法类似,从右边开始查找
index(sub[, start[, end]])
释义:跟 find() 的用法仅一点不同:若索引不到,会抛异常
>>> string = "When I grow up, simplicity is a very happy thing!"
>>> string.index("happy")
37
>>> string.index("simple")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
string.index("simple")
ValueError: substring not found
>>>