Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.