Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Select the correct output of the code:

a = "Year 2022 at All the best"

a = a.split('2')

b = a[0] + ". " + a[1] + ". " + a[3]

print (b)

(a) Year . 0. at All the best

(b) Year 0. at All the best

(c) Year . 022. at All the best

(d) Year . 0. at all the best

 

Answer:

Question 5 splits a string into a list -Teachoo.png

So, the correct answer is (a).

 

Explanation:

The split( ) method in python splits a string into a list . The separator for splitting the string is given in the parentheses of the split( ) method.

In the given code, the string ‘a’ is split into 3 and each split is stored as a list item in a and the separator for splitting the string is 2 . This means that the string is split every time a 2 is encountered in the string. 

So the list becomes {“Year ” , ”0” , “ at All the best”}

Finally, all three list items are printed with a ‘.’ in between.

Therefore, the output is “Year . 0. at All the best”

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo