Python String find() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

find() is a Python library function used to find the index of the first occurrence of a substring from the given string. If find() cannot locate the substring, it returns -1 instead of throwing an exception.

Working of Python String find() method

The Find Function in Python follows a straightforward logic to search for a substring within a given string. Below are the critical aspects of how this method operates:

  • The find() method starts searching for the specified substring from the beginning of the string (or the selected start index).
  • It examines each character in the string to see if it matches the first character of the substring.
  • If a match is found, the method checks the subsequent characters in the string and the substring until the entire substring is verified.
  • If the entire substring is found, the method returns the lowest index of the first character of the substring. If the substring is not found, it returns -1.

Examples of Python String find() method

a) find() With No start and end Argument

Output:

Here, we created a variable str containing a sentence and then called find() on str and gave it 'python' as an argument. So, find() will search for 'python' in str and return the index of its first occurrence in str.

When start and end arguments are not given, then find() starts to look from the beginning and searches till the end of the string.

Output:

Their default values are used if you do not pass start and end arguments. The default value of the start is 0, and the end is length of the string.

b) find() With start and end Arguments

Output

start and end arguments set a range and find() searches for the substring only in that range.

'interests' comes at 16th position, which is between 10 and 30, so for the first print(), the output is 16, and for the second it is -1 as 'interests' is not present between 30 and 40.

Syntax of Python String find() method

  • string is the original sentence/word where you want to look for the substring.

Parameter of Python String find() method

The Find Function in Python has optional parameters that allow developers to customize the search process. Here's an explanation of each parameter:

  • substring is the sentence/word you must search for.
  • start and end are optional parameters. They specify the range where the substring needs to be searched.

Return value of Python String find() method

  • If the substring exists in the string, it will return the index of the first occurrence of substring.
  • If the substring does not exist, it will return -1.

Exceptions of Python String find() method

Certainly! The Find Function in Python is a powerful tool for string manipulation, but it's essential to be aware of potential exceptions.

  • The find() method raises a ValueError if the provided start or end parameter is not an integer.
  • Another common exception is TypeError, which occurs if the find() method is called with an incorrect number of arguments.
  • If the find() method is called on a variable that is None (indicating the absence of an object), it will result in an AttributeError.

Conclusion

  • Find Function in Python returns the first occurrence of the substring in the given string. It will return -1 if the substring is not present.
  • Find Function in Python takes 3 parameters: substring, start, end. The start and end are optional.
  • start and end are helpful when searching for a substring at some specific position inside a string.
  • index() is also a similar function, but it will throw a Value Error if the substring is not found.

See Also: