字符串方法 find 和 index

str.find 查找最小索引

字符串方法 str.find(),Python 官方文档描述如下:

help(str.find)
Help on method_descriptor:

find(...)
    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.

返回子字符串 sub 在 s[start:end] 切片内被找到的最小索引。可选参数 start 与 end 会被解读为切片表示法。如果 sub 未被找到则返回 -1。

只给定 sub 一个参数的话,于是从第一个字符开始搜索到字符串结束;如果,随后给定了一个可选参数的话,那么它是 start,于是从 start 开始,搜索到字符串结束;如果 start 之后还有参数的话,那么它是 end;于是从 start 开始,搜索到 end - 1 结束(即不包含索引值为 end 的那个字符)。

'pythonpython'.find('y')
1
'pythonpython'.find('pt')
-1
'pythonpython'.find('y',5)
7
'pythonpython'.find('y',5,7)
-1

str.rfind 查找最大索引

字符串方法 str.rfind(),Python 官方文档描述如下:

help(str.rfind)
Help on method_descriptor:

rfind(...)
    S.rfind(sub[, start[, end]]) -> int
    
    Return the highest 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.

返回子字符串 sub 在字符串内被找到的最大(最右)索引,这样 sub 将包含在 s[start:end] 当中。可选参数 start 与 end 会被解读为切片表示法。如果未找到则返回 -1。

只给定 sub 一个参数的话,于是从第一个字符开始搜索到字符串结束;如果,随后给定了一个可选参数的话,那么它是 start,于是从 start 开始,搜索到字符串结束;如果 start 之后还有参数的话,那么它是 end;于是从 start 开始,搜索到 end - 1 结束(即不包含索引值为 end 的那个字符)。

'python python'.rfind('on')
11
'python python'.rfind('on',1,7)
4
'python python'.rfind('on',7)
11
'python python'.rfind('no')
-1

str.index 查找最小索引

字符串方法 str.index(),Python 官方文档描述如下:

help(str.index)
Help on method_descriptor:

index(...)
    S.index(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.
    
    Raises ValueError when the substring is not found.

返回子字符串 sub 在 s[start:end] 切片内被找到的最小索引。可选参数 start 与 end 会被解读为切片表示法。类似于 find(),但在找不到 sub 时会引发 ValueError。

只给定 sub 一个参数的话,于是从第一个字符开始搜索到字符串结束;如果,随后给定了一个可选参数的话,那么它是 start,于是从 start 开始,搜索到字符串结束;如果 start 之后还有参数的话,那么它是 end;于是从 start 开始,搜索到 end - 1 结束(即不包含索引值为 end 的那个字符)。

'pythonpython'.index('y')
1
'pythonpython'.index('pt')
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-3-4252de1acf66> in <module>
----> 1 'pythonpython'.index('pt')


ValueError: substring not found
'pythonpython'.index('y',5)
7
'pythonpython'.index('y',5,7)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-5-35705122f03a> in <module>
----> 1 'pythonpython'.index('y',5,7)


ValueError: substring not found

str.rindex 查找最大索引

字符串方法 str.rindex(),Python 官方文档描述如下:

help(str.rindex)
Help on method_descriptor:

rindex(...)
    S.rindex(sub[, start[, end]]) -> int
    
    Return the highest 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.
    
    Raises ValueError when the substring is not found.

返回子字符串 sub 在字符串内被找到的最大(最右)索引,这样 sub 将包含在 s[start:end] 当中。可选参数 start 与 end 会被解读为切片表示法。如果未找到则返回 -1。类似于 rfind(),但在子字符串 sub 未找到时会引发 ValueError。

只给定 sub 一个参数的话,于是从第一个字符开始搜索到字符串结束;如果,随后给定了一个可选参数的话,那么它是 start,于是从 start 开始,搜索到字符串结束;如果 start 之后还有参数的话,那么它是 end;于是从 start 开始,搜索到 end - 1 结束(即不包含索引值为 end 的那个字符)。

'python python'.rindex('on')
11
'python python'.rindex('on',1,7)
4
'python python'.rindex('on',7)
11
'python python'.rindex('no')
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-5-92aeb174dba9> in <module>
----> 1 'python python'.rindex('no')


ValueError: substring not found

猜你喜欢

转载自blog.csdn.net/weixin_46757087/article/details/112487126
今日推荐