W3Basic Logo

Python String find() Method

The str.find() method returns the index of the first occurrence(lowest index of the substring) in the sequence where the substring is found. If it does not find the substring, it returns -1

Example:

str = "Hello World!"

# Find the index position of the sub-string "World"
index = str.find("World")
print(index)

Output

6

Syntax

The Syntax of the str.find() function is:

str.find(sub,start,end)

Here, str is a string.

Parameters

The str.find() function can take three parameters.

  • sub (required) - The substring that needs to be searched in the string
  • start (optional) - The starting index position where the search should start. The default value is 0.
  • end (optional) - The ending index position where the search should stop. Default is the end of the string or length of the string.

Return Value

The str.find() function returns an integer value.

  • if the substring is found in the string, it returns the index of the first occurrence (lowest index) where the substring is found.
  • If the substring doesn't exist in the string, it returns -1

Difference Between index() method find() method

The major difference between index() method find() method is:

  • The index() method raises a ValueError exception if the sub-string is not found in the string, while the find() method returns -1.
  • The index() method is faster than the find() method because it stops searching as soon as it finds the first occurrence of the sub-string.

For Example:

str = "Hello World! How are you doing today?"

# Find the index position of the sub-string "Hello" using index()
index = str.index("Hello")
print(index)

# Find the index position of the sub-string "Hi" using index()
try:
    index = str.index("Hi")
except ValueError as e:
    print(e)

# Find the index position of the sub-string "Hello" using find()
index = str.find("Hello")
print(index)

# Find the index position of the sub-string "Hi" using find()
index = str.find("Hi")
print(index)

Output

0
substring not found
0
-1

Example 1: Find the index position of the first occurrence of a sub-string in a string:

In this example, we are searching for the sub-string "How" in the string "Hello World! How are you doing today?". The find() method starts at the beginning of the string and searches for the first occurrence of the sub-string. Since "How" is found at index position 13, the find() method returns 13.

On the other hand, the substring "Hi" does not exist; hence, the find() method returns -1 as output.

str = "Hello World! How are you doing today?"

# Find the index position of the sub-string "How"
index = str.find("How")
print(index)

# Find the index position of the sub-string "Hi"
index2 = str.find("Hi")
print(index2)

Output

13
-1

Example 2: Find the index position of a sub-string starting from a specific index position:

In this example, we are searching for the sub-string "are" in the string "Hello World! How are you doing today?", but we are starting the search from index position 11.

The find() method starts at index position 11 and searches for the first occurrence of the sub-string. Since "are" is found at index position 17, the find() method returns 17.

str = "Hello World! How are you doing today?"

# Find the index position of the sub-string "are" starting from index 11
index = str.find("are", 11)
print(index)

Output

17

Example 3: Find the index position of a sub-string within a specific range of index positions:

In this example, we are searching for the sub-string "you" in the string "Hello World! How are you doing today?", but we are limiting the search to a specific range of index positions (from index 20 to index 30).

The find() method starts at index position 20 and searches for the first occurrence of the sub-string within the specified range. Since "you" is found at index position 21, the find() method returns 21.

str = "Hello World! How are you doing today?"

# Find the index position of the sub-string "you" starting from index 20 and ending at index 30
index = str.find("you", 20, 30)
print(index)

Output

21

Example 4: Find the index position of a sub-string in a string using a case-insensitive search:

In this example, we perform a case-insensitive search for the sub-string "world" in the string "Hello World! How are you doing today?". To do this, we first use the lower() method to convert the string and the sub-string to lowercase.

Then we call the find() method on the lowercase string, which searches for the first occurrence of the lowercase sub-string. Since "world" is found at index position 6, the find() method returns 6.

str = "Hello World! How are you doing today?"

# Find the index position of the sub-string "world" (case-insensitive search)
index = str.lower().find("world")
print(index)

Output

6

Reference: Python Official Docs

© 2023 W3Basic. All rights reserved.

Follow Us: