LearnFrenzy
Sign in Take a free test

Python Coding Interview Questions · Predict the Output · Question 8

Reversing words and characters Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following Python program?

main.py
s = "  the sky  is blue "
print(repr(" ".join(s.split()[::-1])), repr(s.strip()[::-1][:4]))
  • A 'blue is sky the' 'eulb'
  • B 'eulb si yks eht' 'eulb'
  • C 'blue is sky the' 'eulb' Correct
  • D 'blue is sky the' ' eul'
Correct Answer: Option C

split() drops extra spaces, so the reversed words are joined with single spaces. strip() removes the trailing space before reversing the characters.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links