arrow
arrow
arrow
Identify the output of the following Python program segment: S = 'Python Programming' L = S.split() S = ','.join(L) print(S)
Question

Identify the output of the following Python program segment:
S = 'Python Programming'
L = S.split()
S = ','.join(L)
print(S)

A.

Python Programming

B.

Python,Programming

C.

,Python Programming,

D.

,Python, Programming,

Correct option is B

The given Python code first stores the string "Python Programming" in the variable S. Using the split() method without any argument splits the string at whitespace, resulting in a list of words: ['Python', 'Programming']. Then, the join() method combines these list elements back into a single string using a comma (,) as the separator. Hence, the final output printed is Python,Programming, which corresponds to option (b).
Important Key Points:
  1. split() Function: When used without arguments, split() divides a string based on spaces.
  2. Intermediate List Formed: L = ['Python', 'Programming']
  3. join() Function: ','.join(L) joins list elements using a comma as a separator.
  4. Separator Behavior: The separator appears only between elements, not at the beginning or end.
  5. Final Value of S: S = "Python,Programming"
  6. Printed Output: The print(S) statement displays the final joined string.

Knowledge Booster:

  • Why option (a) is incorrect?
    This would be the original string, but the code modifies it using split() and join().
  • Why option (c) is incorrect?
    join() does not add separators at the beginning or end.
  • Why option (d) is incorrect?
    Extra commas before and after elements are not added by join().
  • Example: '-'.join(['A', 'B', 'C']) # Output: A-B-C

Free Tests

Free
Must Attempt

UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘KVS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow