Python Reference Library →

Python String find() – How to Locate Substrings Easily

Let’s look at string find() – how to locate substrings easily

  1. Notice

    find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'banana'
result = text.find("a")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'banana'Object 2 · int1
This step

find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.

Picture string find() – how to locate substrings easily

find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.

Image brief

Alt text
A diagram for Python String find() – How to Locate Substrings Easily. Instructions: text = 'banana' result = text.find("a") print(result). Displayed result: 1. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'banana'; object 2: 1
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String find() – How to Locate Substrings Easily". Teaching focus: find() returns the first matching substring index, or -1 if absent. Index zero is a valid match. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'banana' result = text.find("a") print(result) | v DISPLAYED RESULT 1 FINAL BINDINGS text --> object 1 (str) result --> object 2 (int) object 1: 'banana' object 2: 1 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String find() – How to Locate Substrings Easily. Instructions: text = 'banana' result = text.find("a") print(result). Displayed result: 1. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'banana'; object 2: 1

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'banana'
result = text.find("a")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'banana'Object 2 · int1
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
1

What do you expect this program to print?

What does this example display?

text = 'banana'
result = text.find("a")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

1

Follow the printed values in order. The output is: 1

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.

find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.

More examples and reference

Python String find() Method

The find() method in Python helps you locate the position of a substring inside another string. It's a safe way to check if a word or character exists and find where it appears first.

Syntax

string.find(substring, start, end)

Parameters:

  • substring – The string you want to find inside the main string (required).
  • start – The position to start the search (optional).
  • end – The position to end the search (optional).

Returns:

  • Index (integer) of the first occurrence of the substring.
  • Returns -1 if the substring is not found.

Example 1: Basic Usage

text = "Python programming is fun"
position = text.find("programming")
print(position)
7

Why? Because the word "programming" starts at index 7 in the string.

Example 2: Substring Not Found

print("apple".find("z"))
-1

Note: find() doesn't crash if the substring isn't found – it simply returns -1.

Example 3: Using Start and End

text = "learn learn learn"
position = text.find("learn", 6)
print(position)
6

Here, we told Python to start looking from index 6. It finds the second "learn" starting at index 6.

Real-Life Use Case

Use find() to check if a user input contains certain keywords:

message = "Please confirm your email address"
if message.find("confirm") != -1:
    print("Confirmation keyword found!")
Confirmation keyword found!

Difference Between find() and index()

  • find() returns -1 if not found
  • index() raises a ValueError if not found

Common Mistakes

  • Forgetting that find() is case-sensitive ("Hello".find("h") returns -1)
  • Using == 0 instead of != -1 when checking if something exists

Summary

  • find() helps locate substrings inside strings
  • Returns index if found, or -1 if not
  • Safe to use even if the substring doesn't exist

Practice Problem

Ask the user to enter a sentence and search for the word "Python". Print the result.

sentence = input("Enter a sentence: ")
index = sentence.find("Python")
if index != -1:
    print("Found 'Python' at index", index)
else:
    print("'Python' not found")

What to remember

Connect what you noticed in the example with the idea below.

Remember

find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.

Watch out for this idea

find() raises ValueError when the substring is absent.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →