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)
- A.
I-N-DIA-N-
- B.
I-DIA-
- C.
I-DIAN
- 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.
Given S = 'INDIAN'.
Apply partition: S.partition('N') -> ('I', 'N', 'DIAN') because the first 'N' is after the initial 'I'.
Concatenate with hyphens: L[0] + '-' + L[1] + '-' + L[2] => 'I' + '-' + 'N' + '-' + 'DIAN' = 'I-N-DIAN'.
print(NS) outputs: I-N-DIAN
Therefore, the correct output is I-N-DIAN.