zywscq

两类函数:

  1. find(),rfind()
  2. index(),rindex()

找到了都返回下标.

find找不到返回-1,index找不到抛出ValueError.

带r的表示从右向左找.

都可以使用第二个参数表示从哪个下标开始找.

a=\'abcdab\'
a.find(\'a\')
Out[3]: 0
a.rfind(\'a\')
Out[4]: 4
a.rfind(\'a\',1)
Out[5]: 4
a.rfind(\'x\')
Out[6]: -1
a.index(\'a\')
Out[7]: 0
a.index(\'a\',1)
Out[8]: 4
a.rindex(\'a\')
Out[9]: 4
a.index(\'x\')
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-51f0d5bb66b2>", line 1, in <module>
    a.index(\'x\')
ValueError: substring not found

 

分类:

技术点:

相关文章:

  • 2021-12-12
  • 2021-11-04
  • 2021-11-20
  • 2021-04-16
  • 2021-12-22
  • 2021-12-12
  • 2021-11-04
  • 2021-10-09
猜你喜欢
  • 2021-11-18
  • 2021-12-27
  • 2021-11-13
  • 2021-11-18
  • 2021-12-22
相关资源
相似解决方案