Question:

Identify the correct output of the following code snippet:
game = "Olympic2024"
print(game.index("C"))

Show Hint

find() returns -1 if the substring is not found,
while index() raises an error.
Be cautious about letter case!
  • 0
  • 6
  • -1
  • ValueError
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is D

Solution and Explanation

In the given string, game = "Olympic2024", we are trying to find the index of the character `"C"`.
However, Python’s string index method is case-sensitive.
The string `"Olympic2024"` contains a lowercase `"c"` at position 6, but not an uppercase `"C"`.
The index() method searches for the exact match and raises a ValueError if the substring is not found.
Unlike the find() method, which returns -1 when not found, index() throws an error.
Since `"C"` (uppercase) does not exist in the string, Python will raise a ValueError.
Therefore, the correct answer is option (D).
Was this answer helpful?
0
0

Top Questions on Programming in Python

View More Questions

Questions Asked in CBSE CLASS XII exam

View More Questions