ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
s = "a-b-c-d"
print(s.split("-", 2), s.rsplit("-", 1))- A. ['a', 'b'] ['d']
- B. ['a', 'b', 'c-d'] ['a-b-c', 'd']
- C. ['a', 'b', 'c-d'] ['a', 'b-c-d']
- D. ['a-b', 'c', 'd'] ['a-b-c', 'd']
Correct answer: B. ['a', 'b', 'c-d'] ['a-b-c', 'd']
The second argument is the maximum number of splits, not the number of pieces. split("-", 2) splits twice from the left giving three pieces with the remainder "c-d" kept whole. rsplit("-", 1) splits once from the right: ['a-b-c', 'd'].
More Python Programming questions
- What is printed? python print("hello world".title(), "ab".center(6, ""), "42".zfill(5), "-7".zfill(4))
- What is printed? python d = dict(zip("abc", range(3))) d.update({"b": 9, "d": 1}) print(list(d.items())[1], sum(d.values()), len(d))
- What is printed? python a = {1, 2, 3, 4} b = {3, 4, 5} print(a - b, b - a, len(a | b), a & b == {4, 3})
- What is printed? python print(" ".join(i ch for i, ch in enumerate("xyz", 1)), list(enumerate("ab"))[-1])
- What is printed? python nums = list(range(10)) print(nums[8:2:-2], nums[-3:], nums[2:8:-1])
- What is printed? python print(min([3, -4, 2], key=lambda v: v v), max({"a": 3, "b": 1}), max("abc", "b", key=len))