Q.Select the correct output of the code: S="Amrit Mahotsav @ 75" A=S.partition(" ") print(A)
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The partition() method splits a string into three parts at the first occurrence of the specified separator, returning a tuple. The output is ('Amrit', ' ', 'Mahotsav @ 75').
The core concept here revolves around understanding how Python's string partition() method works. Unlike split(), which can divide a string into many parts based on a separator and returns a list, partition() has a very specific job: it always divides the string into exactly three parts and returns them as a tuple.
Here's the breakdown of what those three parts are:
- The part of the string before the first occurrence of the separator.
- The separator itself.
- The part of the string after the first occurrence of the separator.
The key is "first occurrence." If the separator appears multiple times, partition() only considers the very first one it finds. If the separator is not found in the string, partition() returns a tuple containing the original string, followed by two empty strings ('', '').
Let's apply this to the given code.
-
Identify the string and the separator.
We have the string
S = "Amrit Mahotsav @ 75".The
partition()method is called withS.partition(" "), meaning the separator we are looking for is a single space character" ". -
Locate the first occurrence of the separator.
In the string "Amrit Mahotsav @ 75", the first space character appears immediately after the word "Amrit".
S = "Amrit Mahotsav @ 75"^(First space) -
Divide the string into three parts based on the first separator.
- Part 1 (before the separator): Everything before the first space is "Amrit".
- Part 2 (the separator itself): The separator is " ".
- Part 3 (after the separator): Everything after the first space is "Mahotsav @ 75".
-
Form the tuple.
The
partition()method returns these three parts as a tuple.So,
Awill be('Amrit', ' ', 'Mahotsav @ 75'). …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.