Select the correct output of the given Python code from the following options:…

2023

Select the correct output of the given Python code from the following options:
S='INDIAN'
L=S.Partition('N')
NS=L[0]+'-'+L[1]+'-'+L[2]
print (NS)

  1. A.

    I-N-DIA-N-

  2. B.

    I-DIA-

  3. C.

    I-DIAN

  4. D.

    I-N-DIAN

Attempted by 1183 students.

Show answer & explanation

Correct answer: D

Key insight: str.partition(sep) returns a tuple of three strings: the part before the first occurrence of sep, the separator itself, and the part after.

  1. Given S = 'INDIAN'.

  2. Apply partition: S.partition('N') -> ('I', 'N', 'DIAN') because the first 'N' is after the initial 'I'.

  3. Concatenate with hyphens: L[0] + '-' + L[1] + '-' + L[2] => 'I' + '-' + 'N' + '-' + 'DIAN' = 'I-N-DIAN'.

  4. print(NS) outputs: I-N-DIAN

Therefore, the correct output is I-N-DIAN.

Explore the full course: Rssb Senior Computer Instructor