Select the correct output of the code:

[Sample Paper] Select the correct output of the code: - Teachoo - CBSE Class 12 Sample Paper for 2024 Boards

Options:

a. PYTHON-IS-Fun

b. PYTHON-is-Fun

c. Python-is-fun

d. PYTHON-Is -Fun

Answer

Answer by student

b. PYTHON-is-Fun.

Detailed answer by teachoo

To find the output of the code, we need to understand how the string methods work in Python. The string methods are:

  • split() : This method splits a string into a list of words based on a separator. If no separator is given, it splits on whitespace.
  • join() : This method joins a list of strings into one string using a separator. The separator is a string that is placed between each element of the list.
  • upper() : This method returns a copy of a string with all letters converted to uppercase.
  • capitalize() : This method returns a copy of a string with only its first letter capitalized and the rest lowercase.

Evaluating the code step by step:

  • s = “Python is fun”

First, we assign the string “Python is fun” to the variable s.

  • l = s.split()

Next, we split s into a list of words using the split() method and assign it to the variable l. So, l becomes ["Python", "is", "fun"] .

  • s_new = “-”.join([l[0][0].upper(), l[1][0], l[2][0].capitalize()])

Then, we join the first letter of each word in l with a hyphen using the join() method and assign it to the variable s_new. 

To get the first letter of each word, we use indexing with [0]

To capitalize the first and third letters, we use the upper() and capitalize() methods respectively. 

So, s_new becomes "PYTHON-is-Fun" .

  • print(s_new)

Finally, we print s_new using the print() function. 

Let’s analyze each of the options and see why they are correct or incorrect.

  • Option a: PYTHON-IS-Fun. This is incorrect because it capitalizes the second word “IS”, which is not what the code does.
  • Option b: PYTHON-is-Fun . This is correct because it matches the output of the code.
  • Option c: Python-is-fun. This is incorrect because it does not capitalize the first word “Python”, which is not what the code does.
  • Option d: PYTHON-Is -Fun. This is incorrect because it has an extra space before the hyphen, which is not what the code does.

So, the correct answer is b. PYTHON-is-Fun.

Remove Ads
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.