difference between find and index
Question:
I am new to python and cannot quite understand the difference between find and index.
>>> line
'hi, this is ABC oh my god!!'
>>> line.find("o")
16
>>> line.index("o")
16
They always return the same result.
Thanks!!
Answers:
str.find
returns -1
when it does not find the substring.
>>> line = 'hi, this is ABC oh my god!!'
>>> line.find('?')
-1
While str.index
raises ValueError
:
>>> line.index('?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Both the functions behave the same way if a sub-string is found.
Also find is only available for strings where as index is available for lists, tuples and strings
>>> somelist
['Ok', "let's", 'try', 'this', 'out']
>>> type(somelist)
<class 'list'>
>>> somelist.index("try")
2
>>> somelist.find("try")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'find'
>>> sometuple
('Ok', "let's", 'try', 'this', 'out')
>>> type(sometuple)
<class 'tuple'>
>>> sometuple.index("try")
2
>>> sometuple.find("try")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'find'
>>> somelist2
"Ok let's try this"
>>> type(somelist2)
<class 'str'>
>>> somelist2.index("try")
9
>>> somelist2.find("try")
9
>>> somelist2.find("t")
5
>>> somelist2.index("t")
5
@falsetru provided an explanation about the difference between functions, I did a performance test between them.
"""Performance tests of 'find' and 'index' functions.
Results:
using_index t = 0.0259 sec
using_index t = 0.0290 sec
using_index t = 0.6851 sec
using_find t = 0.0301 sec
using_find t = 0.0282 sec
using_find t = 0.6875 sec
Summary:
Both (find and index) functions have the same performance.
"""
def using_index(text: str, find: str) -> str:
"""Returns index position if found otherwise raises ValueError."""
return text.index(find)
def using_find(text: str, find: str) -> str:
"""Returns index position if found otherwise -1."""
return text.find(find)
if __name__ == "__main__":
from timeit import timeit
texts = [
"short text to search" * 10,
"long text to search" * 10000,
"long_text_with_find_at_the_end" * 10000 + " to long",
]
for f in [using_index, using_find]:
for text in texts:
t = timeit(stmt="f(text, ' ')", number=10000, globals=globals())
print(f"{f.__name__} {t = :.4f} sec")
print()
I am new to python and cannot quite understand the difference between find and index.
>>> line
'hi, this is ABC oh my god!!'
>>> line.find("o")
16
>>> line.index("o")
16
They always return the same result.
Thanks!!
str.find
returns -1
when it does not find the substring.
>>> line = 'hi, this is ABC oh my god!!'
>>> line.find('?')
-1
While str.index
raises ValueError
:
>>> line.index('?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Both the functions behave the same way if a sub-string is found.
Also find is only available for strings where as index is available for lists, tuples and strings
>>> somelist
['Ok', "let's", 'try', 'this', 'out']
>>> type(somelist)
<class 'list'>
>>> somelist.index("try")
2
>>> somelist.find("try")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'find'
>>> sometuple
('Ok', "let's", 'try', 'this', 'out')
>>> type(sometuple)
<class 'tuple'>
>>> sometuple.index("try")
2
>>> sometuple.find("try")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'find'
>>> somelist2
"Ok let's try this"
>>> type(somelist2)
<class 'str'>
>>> somelist2.index("try")
9
>>> somelist2.find("try")
9
>>> somelist2.find("t")
5
>>> somelist2.index("t")
5
@falsetru provided an explanation about the difference between functions, I did a performance test between them.
"""Performance tests of 'find' and 'index' functions.
Results:
using_index t = 0.0259 sec
using_index t = 0.0290 sec
using_index t = 0.6851 sec
using_find t = 0.0301 sec
using_find t = 0.0282 sec
using_find t = 0.6875 sec
Summary:
Both (find and index) functions have the same performance.
"""
def using_index(text: str, find: str) -> str:
"""Returns index position if found otherwise raises ValueError."""
return text.index(find)
def using_find(text: str, find: str) -> str:
"""Returns index position if found otherwise -1."""
return text.find(find)
if __name__ == "__main__":
from timeit import timeit
texts = [
"short text to search" * 10,
"long text to search" * 10000,
"long_text_with_find_at_the_end" * 10000 + " to long",
]
for f in [using_index, using_find]:
for text in texts:
t = timeit(stmt="f(text, ' ')", number=10000, globals=globals())
print(f"{f.__name__} {t = :.4f} sec")
print()