Q.What will be the output of the following statement ? print("PythonProgram"[-1:2:-2])
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →This slice uses a negative step to walk the string in reverse, from the last character down to (but not including) index 2, taking every second character. The output is mroPo.
Concept — Python string slicing with a negative step
Python's slice syntax is [start:stop:step]:
- start — the index where the slice begins.
- stop — the index where it ends; the character at
stopis not included. - step — the jump between indices. A negative step walks the string from right to left, continuing while the current index is greater than
stop.
A negative index counts from the end: -1 is the last character. For a string of length 13, index -1 is the same as index 12.
Step-by-step trace
The string "PythonProgram" has 13 characters:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Char | P | y | t | h | o | n | P | r | o | g | r | a | m |
For [-1:2:-2]: start = -1 (that is index 12), stop = 2, step = -2. We move backward from index 12, taking every second character, and stop as soon as the index is not greater than 2:
| Current index | Included? (index > 2) | Character taken | …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.