Question:

What will be the output of the following statement?

print("PythonProgram"[-1:2:-2])

Show Hint

Start at the last character, move left two places at a time, and stop before index 2.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Read the slice syntax:
The slice s[start:stop:step] takes characters from start towards stop, but never includes the stop index.
Here start = -1, stop = 2 and step = -2. A negative step means we move from right to left.

Step 2: Number the characters:
The string "PythonProgram" has 13 characters.
Indexes 0 to 12 are: P(0) y(1) t(2) h(3) o(4) n(5) P(6) r(7) o(8) g(9) r(10) a(11) m(12).
The index -1 is the same as 12, so the start is 'm'.

Step 3: Move left by 2 each time:
Index 12 gives 'm'.
Index 10 gives 'r'.
Index 8 gives 'o'.
Index 6 gives 'P'.
Index 4 gives 'o'.
The next index is 2, which is the stop index. It is not included, so we stop.

Step 4: Join the characters:
The characters in order are m, r, o, P, o. Running the statement in Python prints mroPo.

Final Answer:
The output is mroPo.
Was this answer helpful?
0
0