Difference Between find() and index() in Python Last Updated : 21 Nov, 2024 Comments Improve Suggest changes 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 vijayakumarchinthala Follow Improve Article Tags : Strings Python Python Programs DSA python-string Python-string-functions +2 More Practice Tags : pythonStrings Similar Reads Difference Between Enumerate and Iterate in Python In Python, iterating through elements in a sequence is a common task. Two commonly used methods for this purpose are enumerate and iteration using a loop. While both methods allow us to traverse through a sequence, they differ in their implementation and use cases.Difference Between Enumerate And It 3 min read Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform 2 min read Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a 2 min read Handling " No Element Found in Index() " - Python The task of handling the case where no element is found in the index() in Python involves safely managing situations where the desired element does not exist in a list. For example, with the list a = [6, 4, 8, 9, 10] and the element 11, we want to ensure the program doesn't raise an error if the ele 3 min read Python - Find Index containing String in List In this article, we will explore how to find the index of a string in a list in Python. It involves identifying the position where a specific string appears within the list.Using index()index() method in Python is used to find the position of a specific string in a list. It returns the index of the 2 min read Find element in Array - Python Finding an item in an array in Python can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in a Python array.Using the in Operatorin operator is one of the most straightforward ways to check if an item exists in an array. It r 3 min read Python - Distance between occurrences Sometimes, while working with Python Strings, we can have a task in which we need to find the indices difference between occurrences of a particular character. This can have applications in domains such as day-day programming. Let us discuss certain ways in which this task can be done. Method #1: Us 5 min read Key Index in Dictionary - Python We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp 2 min read Python | Check for Nth index existence in list Sometimes, while working with lists, we can have a problem in which we require to insert a particular element at an index. But, before that it is essential to know that particular index is part of list or not. Let's discuss certain shorthands that can perform this task error free. Method #1 : Using 3 min read Python | First alphabet index Sometimes, while working with Python strings we can have a problem in which we need to extract the first index of alpha character from string. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed. Method #1: Using loop + regex The combinatio 6 min read Like