By Lenin Mishra
If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.
find() method in Python, finds the first occurrence of a specified value in a given string. It returns the index where the substring
It returns -1 if the value is not found.
find() method is similar to the index() method.
Only difference -
index() method raises an exception if the value is not found.
It is not case sensitive.
string.find(value, start, end) value: The value to search for(Required) start: The integer position to start the search. Default is 0.(Optional) end: The integer position to end the search. Default is the end of the string.(Optional)
s2 = 'I like Pylenin' s1 = 'like' print(s2.find(s1))
The above result tells us that the
like substring first occurs at 2nd index.
Let’s check for another substring.
s2 = 'I like Pylenin' s1 = 'Python' print(s2.find(s1))
The above result tells us that the substring
Python doesn’t exist in
Example 3 - Check for case sensitivity
s2 = 'I like Pylenin' s1 = 'pylenin' print(s2.find(s1))
Check out other commonly used Python string methods.
- How to create a string in Python?
- How to access characters in a Python string?
- How to replace characters in a string in Python?
- How to concatenate strings in Python?
- How to iterate through a string in Python?
- Check if a Substring is Present in a Given String in Python
- Escape sequences in Python String
- Python String Formatting - The Definitive Guide