字符串find方法的知识点

今天给大家说下python字符串的find方法,从python的文档里可以知道find方法是查找子串在字符串的开始位置。

看下文档解释:

string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices

后面的[]是表示可选项,start,end表示查找字符串的开始位置和结束位置。

比如下面的例子:
info = 'abca'
print info.find('a')

返回的结果是:0

info = 'abca'
print info.find('a',1)

返回的结果是:3

info = 'abca'
print info.find('333')
返回的结果是:-1

有兴趣的可以看看python 字符串 index方法。

 

转载自:http://www.cnpythoner.com/post/226.html

posted @ 2013-01-15 21:09  老王python  阅读(1410)  评论(0编辑  收藏  举报