Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'banana'
result = text.find("a")
print(result)Top-level scope
'banana'Object 2 · int1find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.
(no output)
What do you expect this program to print?
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: 1Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'banana'
result = text.find("a")
print(result)Top-level scope
'banana'Object 2 · int1The example has finished. Compare the final output with the values and references above.
1
What do you expect this program to print?
text = 'banana'
result = text.find("a")
print(result)String find() MethodThe 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.
string.find(substring, start, end)
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).-1 if the substring is not found.text = "Python programming is fun"
position = text.find("programming")
print(position)
7
Why? Because the word "programming" starts at index 7 in the string.
print("apple".find("z"))
-1
Note: find() doesn't crash if the substring isn't found – it simply returns -1.
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.
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!
find() returns -1 if not foundindex() raises a ValueError if not foundfind() is case-sensitive ("Hello".find("h") returns -1)== 0 instead of != -1 when checking if something existsfind() helps locate substrings inside strings-1 if notAsk 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")
Connect what you noticed in the example with the idea below.
find() returns the first matching substring index, or -1 if absent. Index zero is a valid match.
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.