SohamC:
我是python的新手,无法完全理解find和index之间的区别。
>>> line
'hi, this is ABC oh my god!!'
>>> line.find("o")
16
>>> line.index("o")
16
他们总是返回相同的结果。谢谢!!
错误:
str.find
-1
找不到子字符串时返回。
>>> line = 'hi, this is ABC oh my god!!'
>>> line.find('?')
-1
虽然str.index
加注ValueError
:
>>> line.index('?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
如果找到子字符串,则两个函数的行为相同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
编辑于
我来说两句