LearnFrenzy
Sign in Take a free test

Strings · Predict the Output · Question 4

split() and join() Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following Python program?

main.py
print("a,b,,c".split(","), " a  b ".split(), "-".join(["x", "y", "z"]))
  • A ['a', 'b', '', 'c'] ['a', 'b'] x-y-z Correct
  • B ['a', 'b', 'c'] ['a', 'b'] x-y-z
  • C ['a', 'b', '', 'c'] ['', 'a', '', 'b', ''] x-y-z
  • D ['a', 'b', '', 'c'] ['a', 'b'] -x-y-z-
Correct Answer: Option A

With an explicit separator, two commas in a row give an empty string. split() with no argument collapses whitespace. join puts the separator only between items.

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