Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python.find()字符串方法返回什么?_Python - Fatal编程技术网

Python.find()字符串方法返回什么?

Python.find()字符串方法返回什么?,python,Python,我想知道能否从您那里得到一些关于python中.find()字符串方法的帮助。下面是一个例子: word = 'banana' index = word.find('a') 1#result word.find('na') 2#result word.find('na', 3) 4#result name = 'bob' name.find('b', 1, 2) -1#result 你能解释一下这个字符串方

我想知道能否从您那里得到一些关于python中.find()字符串方法的帮助。下面是一个例子:

    word = 'banana'
    index = word.find('a')
    1#result
    word.find('na') 
    2#result
    word.find('na', 3)
    4#result 
    name = 'bob' 
    name.find('b', 1, 2) 
    -1#result
你能解释一下这个字符串方法具体做什么吗? 同样对于word.find('na')和word.find('b',1,2')这两部分,运行这些命令时的数字是什么,结果的含义是什么??谢谢你的帮助

In [1]: print str.find.__doc__
S.find(sub [,start [,end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.

用谷歌搜索你不懂的文档字符串中的所有单词,你就会得到答案

数字是索引。因此,在上一个示例中,它在
'bob'[1:2]
中搜索'b',但没有找到它(因为
'bob'[1:2]
是'o'):