PCEP-30-02 · Question #275
Which of the following is the output of the below Python code? ``python str = 'Hello World' print(str[::-1]) ``
The correct answer is B. dlroW olleH. str[::-1] uses Python's slice notation with a step of -1, which iterates through the string in reverse order - producing dlroW olleH, making B correct. Why the distractors are wrong: A (Hello World) would result from printing str with no slicing at all. C (World) would require…
Question
str = 'Hello World'
print(str[::-1])
Options
- AHello World
- BdlroW olleH
- CWorld
- DHello
How the community answered
(34 responses)- A15% (5)
- B76% (26)
- C6% (2)
- D3% (1)
Explanation
str[::-1] uses Python's slice notation with a step of -1, which iterates through the string in reverse order - producing dlroW olleH, making B correct.
Why the distractors are wrong:
- A (
Hello World) would result from printingstrwith no slicing at all. - C (
World) would require slicing likestr[6:]to extract just the second word. - D (
Hello) would require slicing likestr[:5]to extract just the first word.
Memory tip: Think of [::-1] as "flip it" - the -1 step means "go backwards one character at a time from end to start." Any time you see a negative step in a slice, the string is being reversed.
Community Discussion
No community discussion yet for this question.