Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 30 of 102
Marks: +1, -0
Identify the correct output of the following code snippet :
game="Olympic2024"
print(game.index("C"))
[CBSE 2025 Set 4]
Solution:  
Concept:
str.index(sub) returns the lowest index where sub is found. If sub is not present, it raises a ValueError. (str.find(sub) returns -1 in that case.)
Explanation:
String comparison is case-sensitive. "Olympic2024" contains a lowercase 'c' at index 6, but no uppercase 'C'. So index("C") fails:
ValueError: substring not found
Key point: 6 would be the answer for game.index("c"), and -1 is what find() returns, not index().
Answer:
Option D (ValueError)
© examsnet.com
Advertisements
Go to Question: