nerdexam
Python_Institute

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

Which of the following is the output of the below Python code?
str = 'Hello World'
print(str[::-1])

Options

  • AHello World
  • BdlroW olleH
  • CWorld
  • DHello

How the community answered

(34 responses)
  • A
    15% (5)
  • B
    76% (26)
  • C
    6% (2)
  • D
    3% (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 printing str with no slicing at all.
  • C (World) would require slicing like str[6:] to extract just the second word.
  • D (Hello) would require slicing like str[: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.

Full PCEP-30-02 Practice