KnowledgeBoat Logo
|

Computer Science

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"]

Python String Manipulation

2 Likes

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'].

Answered By

2 Likes


Related Questions