What is the output of the following Python code? s = “Python”; print(s, s)
Correct answer B. yth on
Explanation
Slicing s[start:stop] includes the start index but excludes the stop index, so s[1:4] takes indexes 1, 2 and 3, giving "yth". Negative indexes count from the end, so s[-2:] starts at the second-last character and runs to the end, giving "on". Including index 4 to get "ytho" is the usual off-by-one mistake.
Answer checked How we write and check questions
Discussion