Remember
index() returns the first matching substring index. It raises ValueError when no match exists.
index() returns the first matching substring index. It raises ValueError when no match exists.
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.index("a")
print(result)Top-level scope
'banana'Object 2 · int1index() returns the first matching substring index. It raises ValueError when no match exists.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'banana'
result = text.index("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.index("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.index("a")
print(result)index() MethodThe index() method in Python is used to find the position of the first occurrence of a substring inside a string. If the substring is not found, it raises a ValueError.
string.index(substring, start, end)
substring – The string to search for.start – (Optional) The position to start searching from. Default is 0.end – (Optional) The position to stop searching. Default is end of the string.text = "Python is fun"
print(text.index("fun"))
10
Explanation: The word "fun" starts at index 10 in the string.
text = "banana"
print(text.index("a", 2))
3
Explanation: It starts searching from index 2, so it finds the second "a" at position 3.
index() raises a ValueError if the substring is not found.find() returns -1 instead of raising an error.text = "hello"
print(text.index("z")) # Raises ValueError
ValueError: substring not found
text = "apple pie"
if "pie" in text:
print("Found at index:", text.index("pie"))
Found at index: 6
string.index() returns the index of the first occurrence of a substring.ValueError if not found.start and end to limit the search range.Write a program to find the position of the word "world" in the string "hello world" using index().
text = "hello world"
print("Index of 'world':", text.index("world"))
6
Try using index() with different substrings and edge cases like empty strings or overlapping matches.
Connect what you noticed in the example with the idea below.
index() returns the first matching substring index. It raises ValueError when no match exists.
index() returns -1 when no match exists.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.