What is the output of the following code snippet ?
``python
s='War and Peace by Leo Tolstoy'
print(s.partition("by"))
``
What is the output of the following code snippet ?
``python
s='War and Peace by Leo Tolstoy'
print(s.partition("by"))
``
Options
Option A — ('War and Peace ', 'by', ' Leo Tolstoy') is correct, because str.partition(sep) always returns a 3-element tuple: (text before separator, the separator itself, text after separator).
Marking Scheme
- 11 mark: for correctly selecting option A.
Hint
partition() always returns a 3-element TUPLE: (before, separator, after).
Quick Oral Answer
partition() splits at the first occurrence of the separator and returns a 3-element tuple (before, separator, after), so the answer here is the tuple with 'War and Peace ', 'by', and ' Leo Tolstoy'.
Analysis & Explanation
partition() is a string method with a fixed, predictable 3-part return structure that is frequently tested in CBSE MCQs.
Concept
s.partition(sep)splits the string at the FIRST occurrence ofsepand returns exactly 3 items as a tuple (never a list):(before, sep, after).- For
s = 'War and Peace by Leo Tolstoy'andsep = "by": before ='War and Peace ', separator ='by', after =' Leo Tolstoy'.
Why other options are wrong
- Option B — uses square brackets
[...], implying a list; partition() always returns a tuple, not a list. - Option C — has only 2 elements and omits the separator itself; partition() always includes the separator as the middle element even when found.
- Option D — uses square brackets (list) AND omits the separator — combines both errors above.
Exam trap
- Students often confuse
partition()(always 3-tuple, separator included) withsplit()(returns a list, separator excluded).
Common Mistakes
- 1Confusing partition()'s tuple output with split()'s list output.
- 2Forgetting that the separator itself is included as the middle element of the returned tuple.
Interesting Facts
If the separator is not found in the string, partition() returns (original_string, '', '') — the original string followed by two empty strings.
There is also an rpartition() method that splits at the LAST occurrence of the separator instead of the first.
Spotted a mistake or something unclear?
Tell us — we fix reported answers fast.
Frequently Asked Questions
What does partition() return if the separator is not found?
It returns a tuple of (original_string, '', '') — the whole string as the first element and two empty strings for the separator and the remainder.
What is the difference between partition() and split()?
partition() splits only at the first occurrence and returns a 3-element tuple including the separator; split() can split at every occurrence (or a limited count) and returns a list without the separator.