CBSE Class 11 Computer Science Question 3 of 19

Strings in Python — Question 13

Back to all questions
13
Question

Question 13

What will be the output of the following?

print("ComputerScience".split("er", 2))
  1. ["Computer", "Science"]
  2. ["Comput", "Science"]
  3. ["Comput", "erScience"]
  4. ["Comput", "er", "Science"]
Answer

["Comput", "Science"]

Reason — The split() method splits a string into a list of substrings based on the specified delimiter. In this case, "er" is the delimiter. The second argument 2 is the maximum number of splits to be made. "ComputerScience" is split using "er" as the delimiter. The first occurrence of "er" splits "ComputerScience" into "Comput" and "Science". Since the 2 splits limit is applied, it does not continue searching for additional delimiters beyond the first one. Thus, the output is ['Comput', 'Science'].