Question Detail

COS 102 Advanced Text Processing Objective Question

Question

Given the Python code snippet:
word = "Python"
print(word[0:2])
What will be the output?
Options
A P
B Py
Correct Answer
C yth
D Python
Correct Answer

Option B is the correct answer.

Detailed Explanation

String slicing word[start:stop] includes the character at the start index but excludes the character at the stop index. So, word[0:2] includes index 0 ('P') and index 1 ('y'), but excludes index 2 ('t').

Hint

Remember the 'Off-By-One Reminder' for slicing: the stop index is excluded.

Question Info