In this tutorial, we’ll learn what Python string find() method is and how to properly use it with the help of an easy and detailed Python code examples.
Outline
- Introduction: Python String find() Method
- Syntax of find() Method
- Example 1: find() Method with no Specific Range
- Example 2: Substring not Found
- Example 3: find() Method with Specific Range
- Example 4: find() Method Return Value
- Conclusion
Introduction: Python String find() Method
This method is used to return the index of first occurrence of substring in a string. If the substring is not found then it returns -1.
Syntax of find() Method
string.find(subString, startingPoint, endingPoint)
- Python string find() method takes three arguments.
- First argument is a substring that is to be searched within a specified string.
- Second argument is optional. It’s used to specify from where the search should start.
- Third argument is also optional. It specified the ending point of search.
- This find() method returns the index value of first occurence of substring if its found in the string. If its not found then it returns -1.
Example 1: find() Method with no Specific Range
stringval='This is python Programming. Python is good' print(stringval.find('Python'))
Output
28
As you can see that this method is case sensitive. It searched for the Python with capital P(uppercase) and after this substring is found then this method returns the index value of first character of this substring.
In order to make it case insensitive, we’ve to use lower() or upper() method. See below code:
stringval='This is Python Programming. Python is good' print(stringval.lower().find('python')) print(stringval.upper().find('PYTHON'))
Output
8 8
Just make sure that when you’re using lower() method then pass the substring in lowercase as well for desired results. Same goes for upper() method as well(substring in uppercase).
Example 2: Substring not Found
stringval='This is python Programming.' print(stringval.find('language'))
Output
-1
The specified substring is not found in the string so Python string find() method return -1.
Example 3: find() Method with Specific Range
stringval='This is python Programming.' print(stringval.find('python',10)) // the string will start from ( ython Programming).
Output
-1
The substring is present but still the output shows that its not. Reason is that the starting point from where the search starts don’t have all the characters of specified substring as shown in the above code.
We can also specify ending range as well. See below code:
stringval='This is python Programming.' print(stringval.find('python',2,16))
Output
8
By using this method, we can create a specific range within string to execute the search for a substring.
Example 4: find() Method Return Value
stringval='This is python Programming.' returnedVal=stringval.find('python') // return 8 returnedVal2=stringval.find('Flutter') // return -1 (not found) print(returnedVal) print(returnedVal2)
Output
8 -1
As you can see here that Python string find() method returns the index of first occurence of substring and returns -1 if not found.
Do ask if you still have any questions regarding the implementation of Python string find() method. I’ll be very delighted to answer all your questions.
Conclusion
To conclude this tutorial, hope you now have a detailed practical knowledge of how to properly use Python string find method. I’ll be looking forward to receive your feedback. Thank you for reading this post.
You may like to read:
How To Use Python String Count() Method – Easy Python Code Examples
How To Use Python String CaseFold() Method – Easy Python Code Examples
How To Use Python Dictionary PopItem() Method – Easy Python Guide