Difference Between find() and index() in Python Last Updated : 21 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Both methods are used to locate the position of a substring within a string but the major difference between find() and index() methods in Python is how they handle cases when the substring is not found. The find() method returns -1 in such cases, while index() method raises a ValueError.Example of find() Python s = "hello world" # Finds the position of the substring "world" print(s.find("world")) # Returns -1 when substring is not found print(s.find("python")) # Finds the position of "o" starting from index 5 print(s.find("o", 5)) Output6 -1 7 Explanation:s.find("world") returns 6 because the substring "world" starts at index 6.s.find("python") returns -1 as substring "python" is not present in string.s.find("o", 5) starts the search from index 5 and finds the first occurrence of "o" at index 7.Example of index() Python s = "hello world" # Finds position of the substring "world" print(s.index("world")) # Raises a ValueError when substring is not found print(s.index("python")) # Finds position of "o" starting from index 5 print(s.index("o", 5)) 6 ERROR! Traceback (most recent call last): File "<main.py>", line 7, in <module> ValueError: substring not foundExplanation:s.index("world") returns 6 because substring "world" starts at index 6.When s.index("python") is executed then it raises a ValueError as the substring "python" is not in string.s.index("o", 5) starts searching from index 5 and finds the first occurrence of "o" at index 7.When to use find() and index()? Use find() when we need to check if a substring exists and do not want to handle exceptions.Use index() when we are sure that the substring exists or we want an exception to be raised if it doesn't exist. Comment More infoAdvertise with us Next Article Difference Between find() and index() in Python V vijayakumarchinthala Follow Improve Article Tags : Python python-string Python-string-functions Practice Tags : python Similar Reads Python: Difference between dir() and help() In Python, the dir() and help() functions help programmers understand objects and their functionality.dir() lists all the attributes and methods available for an object, making it easy to explore what it can do.help() provides detailed information about an object, including descriptions of its metho 4 min read Difference between List and Array in Python In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.Operations Difference in Lists and ArraysAccessi 6 min read Python | Pandas Index.difference() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas Index.difference() function return a new Index with elements from the index that 2 min read Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the 3 min read Python List index() - Find Index of Item index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example:Pythona = ["cat", "dog", "tiger" 3 min read Like