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”

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.