Python String find() Method – Tutorial with Examples

Python String find() Method

The find() method is a built-in method in Python that searches for a substring in a string and returns the index of the first occurrence of the substring. If the substring is not found, it returns -1.

Syntax

The syntax of the find() method is as follows:

string.find(substring, start, end)

Here, string is the string to be searched, substring is the substring to be searched for, start is the starting index of the search, and end is the ending index of the search. The start and end parameters are optional, and if not specified, the entire string will be searched.

Return Value

The find() method returns the index of the first occurrence of the substring in the string. If the substring is not found, it returns -1.

Examples

Here are three different examples of how to use the find() method in Python:

Example 1: Finding a Substring in a String

The following example demonstrates how to use the find() method to search for a substring in a string:

string = "The quick brown fox jumps over the lazy dog"
result = string.find("fox")
print(result)

Output:

16

In this example, we use the find() method to search for the substring “fox” in the string “The quick brown fox jumps over the lazy dog”. The method returns the index of the first occurrence of the substring, which is 16.

Example 2: Specifying a Start Index

The following example demonstrates how to use the find() method to search for a substring in a string starting from a specific index:

string = "The quick brown fox jumps over the lazy dog"
result = string.find("the", 10)
print(result)

Output:

31

In this example, we use the find() method to search for the substring “the” in the string “The quick brown fox jumps over the lazy dog” starting from index 10. The method returns the index of the first occurrence of the substring after index 10, which is 31.

Example 3: Handling a Substring Not Found

The following example demonstrates how to handle the case where the substring is not found in the string:

string = "The quick brown fox jumps over the lazy dog"
result = string.find("cat")
print(result)

Output:

-1

In this example, we use the find() method to search for the substring “cat” in the string “The quick brown fox jumps over the lazy dog”. Since the substring is not found in the string, the method returns -1.

Use Cases

The find() method can be used in various scenarios, such as:

  • Searching for a specific word or phrase in a text document.
  • Checking if a certain substring exists in a larger string.
  • Extracting a specific portion of a string based on the location of a substring.
  • Verifying user input in a web form by checking for the presence of specific characters or words.

The find() method is a very useful tool for working with strings in Python. Whether you’re working with text data or user input, the ability to quickly search for and locate specific substrings can save you time and make your code more efficient.

Leave a Reply

Your email address will not be published. Required fields are marked *