Python Strings · Question 17 · Hard

split() and rsplit() are each limited to one split. What does this print?

s = "a-b-c"
print(s.split("-", 1), s.rsplit("-", 1))
  1. ['a', 'b', 'c'] ['a', 'b', 'c']
  2. ['a', 'b-c'] ['a', 'b-c']
  3. ['a', 'b-c'] ['a-b', 'c']
  4. ['a-b', 'c'] ['a', 'b-c']
Answer and explanation

Answer: C. ['a', 'b-c'] ['a-b', 'c']

Explanation: The second argument is the maximum number of splits. split() cuts at the first hyphen from the left, giving ['a', 'b-c'], while rsplit() cuts at the first hyphen from the right, giving ['a-b', 'c'].

Previous: A slice with negative start and negative step is taken from a string. What is… · Next: strip() is given a string of characters as its argument. What does this print?

Practise python strings

Try a fresh set of python strings questions with an explanation after every answer, free and without signing up: Python Strings practice. Or sit a full placement mock test.

More python strings questions

More python practice

Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge