Table of contents
1.
Introduction
2.
Definition and Usage
3.
Syntax
4.
Parameters
5.
Return Value
6.
Example
6.1.
Example:
7.
Using find() with Start and End Parameters
8.
More Examples
8.1.
Example: Finding the First Occurrence of a Letter
8.2.
Example: Searching Between Specific Positions
8.3.
Example: Behavior When Value Is Not Found
9.
Case-Sensitive Behavior of find()
10.
Frequently Asked Questions
10.1.
What if the substring occurs more than once in the string?
10.2.
Is the find function case sensitive?
10.3.
Can I use the find function on lists or other data types?
11.
Conclusion
Last Updated: Dec 20, 2024
Easy

Find Function in Python

Author Gaurav Gandhi
0 upvote

Introduction

The find method in Python is a built-in string method that helps in locating the first occurrence of a substring within a given string. This function proves to be very useful in string manipulation and analysis.

Find Function in Python

In this article, you will learn about the find function, its syntax, usage, and examples to master its application.

Definition and Usage

The find function in Python is used to search for a specified substring within a string. It returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. This method is case-sensitive, meaning uppercase and lowercase letters are treated differently.

Key Features:

  1. It helps locate substrings in larger strings efficiently.
     
  2. Returns -1 if the substring is not found.
     
  3. Allows searching within a specific range of positions.

Syntax

The syntax of the find function is straightforward:

string.find(substring, start, end)

Parameters

  • substring (required): The string to search for.
     
  • start (optional): The position to start the search. Defaults to 0.
     
  • end (optional): The position to end the search. Defaults to the length of the string.

Return Value

  • The index of the first occurrence of the substring.
     
  • Returns -1 if the substring is not found.

Example

Here is a basic example to illustrate the use of the find function:

text = "Hello, welcome to Coding Ninjas!"
result = text.find("welcome")
print("The index of 'welcome' is:", result)
You can also try this code with Online Python Compiler
Run Code


Output:

The index of 'welcome' is: 7


Explanation

The substring "welcome" is found at index 7 in the given string.

Example:

text = "Coding Ninjas welcomes you!"
result = text.find("Ninjas", 0, 15)
print("The index of 'Ninjas' is:", result)
You can also try this code with Online Python Compiler
Run Code


Output:

The index of 'Ninjas' is: 7


Explanation

The search is limited to the range 0-15, and the substring "Ninjas" is found at index 7.

Using find() with Start and End Parameters

The find() function in Python allows you to specify optional start & end parameters to narrow down the search range within the string. The start parameter indicates the index position from where the search should begin, while the end parameter specifies the index position where the search should stop (exclusive). 

The syntax is:

string.find(substring, start, end)


 Let's look at an example:

text = "Hello, world! Hello, Python!"
substring = "Hello"
first_occurrence = text.find(substring)
print(first_occurrence)  
second_occurrence = text.find(substring, 7)
print(second_occurrence)  
You can also try this code with Online Python Compiler
Run Code


Output

0
14


In this code, we have a string text & we want to find the occurrences of the substring "Hello". The first call to find() without specifying start & end parameters returns 0, indicating that "Hello" is found at the beginning of the string.

The second call to find() specifies a start parameter of 7, which means the search starts from index 7 onwards. It returns 14, indicating that the second occurrence of "Hello" is found at index 14 in the string.

You can also specify both start & end parameters to limit the search range:

text = "Hello, world! Hello, Python!"
substring = "Hello"
occurrence = text.find(substring, 0, 10)
print(occurrence) 
You can also try this code with Online Python Compiler
Run Code

 

Output: 

0


In this case, the search is limited to the range from index 0 to index 9 (end parameter is exclusive), & it returns 0 since "Hello" is found within that range.

Note: With the start & end parameters, you can efficiently search for substrings within specific portions of a string, giving you more control over the search process.

More Examples

Example: Finding the First Occurrence of a Letter

text = "banana"
result = text.find("a")
print("The first occurrence of 'a' is at index:", result)
You can also try this code with Online Python Compiler
Run Code


Output:

The first occurrence of 'a' is at index: 1


Explanation

The first "a" appears at index 1.

Example: Searching Between Specific Positions

text = "Python programming is fun."
result = text.find("is", 10, 25)
print("The index of 'is' between positions 10 and 25 is:", result)
You can also try this code with Online Python Compiler
Run Code


Output:

The index of 'is' between positions 10 and 25 is: 18


Explanation

The search starts from index 10 and ends at index 25, where "is" is found at 18.

Example: Behavior When Value Is Not Found

text = "Learning Python is interesting."
result = text.find("Java")
print("The result of searching 'Java' is:", result)
You can also try this code with Online Python Compiler
Run Code


Output:

The result of searching 'Java' is: -1


Explanation: 

The substring "Java" is not present in the string, so the function returns -1.

Case-Sensitive Behavior of find()

It's important to note that the find() function in Python is case-sensitive. This means that it differentiates between uppercase & lowercase letters when searching for substrings.

Let's take an example:

text = "Hello, World!"
substring1 = "hello"
substring2 = "Hello"
occurrence1 = text.find(substring1)
print(occurrence1) 
occurrence2 = text.find(substring2)
print(occurrence2)
You can also try this code with Online Python Compiler
Run Code


Output

-1
0


In this code, we have a string text with the value "Hello, World!". We search for two different substrings: "hello" (lowercase) & "Hello" (uppercase).

When searching for "hello", the find() function returns -1, indicating that the substring is not found in the string. This is because "hello" with lowercase letters does not match the actual substring "Hello" with uppercase 'H' in the text.

On the other hand, when searching for "Hello", the find() function returns 0, indicating that the substring is found at the beginning of the string. The case of the letters matches exactly, so the substring is successfully located.

To perform a case-insensitive search, you can convert both the string & the substring to lowercase (or uppercase) before using the find() function. 

For example:

text = "Hello, World!"
substring = "heLLo"

text_lower = text.lower()
substring_lower = substring.lower()

occurrence = text_lower.find(substring_lower)
print(occurrence)  
You can also try this code with Online Python Compiler
Run Code


Output

0

 

In this case, we convert both the text & the substring to lowercase using the lower() function before performing the search. The find() function then returns 0, indicating that the substring "hello" is found at the beginning of the string, regardless of the original case.

Note: By default, the find() function is case-sensitive, so it's crucial to keep this behavior in mind when searching for substrings. If case-insensitivity is desired, you can convert the strings to a consistent case before applying the find() function.

Frequently Asked Questions

What if the substring occurs more than once in the string?

The find function returns only the index of the first occurrence of the substring.

Is the find function case sensitive?

Yes, the find function is case sensitive. Consider searching for "Python" and "python" within the same string. This would return two different answers.

Can I use the find function on lists or other data types?

No, the find function is specifically for strings. For lists, you may use methods like index().

Conclusion

The find function in Python is one of the most powerful functions to perform string searches. By knowing its syntax, parameters, and use cases, you can effectively tackle any kind of string manipulation tasks. Whether you're working on college projects or are preparing for interviews, you'll definitely find this to be highly useful.

Live masterclass