Returns the index of the first occurrence of the specified substring. Minimal Example>>> 'hello world'.find('or') 7As you read over the explanations below, feel free to watch our video guide about this particular string method: Python String Methods [Ultimate Guide] Syntax and Explanationstr.find(sub[, start[, end]]) Returns the index of the first occurrence of the specified substring. In particular, return the lowest index in the string where sub is found within the slice str[start:end]. You can set the optional arguments start and end as in standard Python slicing. If it doesn’t find any occurrence of the substring in the original string, it returns -1. More String MethodsPython’s string class comes with a number of useful additional string methods. Here’s a short collection of all Python string methods—each link opens a short tutorial in a new tab.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students. To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide. His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here. The index() method returns the index of the first occurence of a substring in the given string. It is same as the find() method except that if a substring is not found, then it raises an exception. Syntax:str.index(substr, start, end)Parameters:
Return Value:An integer value indicating an index of the specified substring. The following examples demonstrates index() method.
greet='Hello World!' print('Index of H: ', greet.index('H')) print('Index of e: ', greet.index('e')) print('Index of l: ', greet.index('l')) print('Index of World: ', greet.index('World'))
Index of H: 0 Index of e: 1 Index of l: 2 Index of World: 6 The index() method returns an index of the first occurance only.
greet='Hello World' print('Index of l: ', greet.index('l')) print('Index of o: ', greet.index('o'))
Index of l: 2 Index of o: 4 The index() method performs case-sensitive search. It throws ValueError if a substring not found.
greet='Hello World' print(greet.index('h')) # throws error: substring not found print(greet.index('hello')) # throws error
ValueError: substring not found ValueError: substring not found If a given substring could not be found then it will throw an error.
mystr='TutorialsTeacher is a free online learning website' print(mystr.index('python'))
Traceback (most recent call last) <ipython-input-3-a72aac0824ca> in <module> 1 str='TutorialsTeacher is a free online learning website' ----> 2 print(str.index('python')) ValueError: substring not found Use start and end parameter to limit the search of a substring between the specified starting and ending index.
mystr='tutorialsteacher is a free tutorials website' print(mystr.index('tutorials',10)) # search starts from 10th index print(mystr.index('tutorials',1, 26)) # throws error; searches between 1st and 26th index
27 ValueError: substring not found |