Python String find() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

The find() method in Python, finds the first occurrence of a specified value in a given string. It returns the index where the substring s1 occurs. It returns -1 if the value is not found.

The 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.

Syntax of find() method
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)
Example 1

Code

s2 = 'I like Pylenin'
s1 = 'like'

print(s2.find(s1))

Output

2

The above result tells us that the like substring first occurs at 2nd index.

Example 2

Let’s check for another substring.

Code

s2 = 'I like Pylenin'
s1 = 'Python'

print(s2.find(s1))

Output

-1

The above result tells us that the substring Python doesn’t exist in s1.

Example 3 - Check for case sensitivity

Code

s2 = 'I like Pylenin'
s1 = 'pylenin'

print(s2.find(s1))

Output

-1

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide