What will be the output of the following code snippet ?
``python
t = tuple('tuple')
t2 = t[2],
t += t2
print(t)
``
What will be the output of the following code snippet ?
``python
t = tuple('tuple')
t2 = t[2],
t += t2
print(t)
``
Options
Option C — ('t', 'u', 'p', 'l', 'e', 'p') is correct: the original tuple formed from the string 'tuple' has its 3rd element ('p') extracted, wrapped in a single-element tuple, and concatenated back onto the original tuple.
Marking Scheme
- 11 mark: for correctly selecting option C.
Hint
A trailing comma after a value creates a single-element tuple — `t[2],` is a tuple `('p',)`, not the string `'p'`.
Quick Oral Answer
tuple('tuple') gives ('t','u','p','l','e'); t[2], with the trailing comma creates the 1-element tuple ('p',); concatenating gives ('t','u','p','l','e','p').
Analysis & Explanation
This question tests two combined tuple concepts: converting a string to a tuple, and the 'trailing comma' trick for creating a single-element tuple.
Concept / trace
t = tuple('tuple')converts the string into a tuple of its characters:('t', 'u', 'p', 'l', 'e').t2 = t[2],— note the trailing comma aftert[2].t[2]is'p'(0-indexed: t(0), u(1), p(2), l(3), e(4)); the trailing comma makest2a 1-element tuple:('p',), NOT just the string'p'.t += t2concatenates the two tuples:('t','u','p','l','e') + ('p',)=('t', 'u', 'p', 'l', 'e', 'p').print(t)displays this final 6-element tuple.
Why other options are wrong
- Option A (
('tuple')) — this is not a valid trace of the code's logic;('tuple')without a comma is just the string 'tuple' in parentheses, not a tuple. - Option B (
('tuple','p')) — wrongly treatstas if it were never converted into individual characters, merging it as one item plus 'p'. - Option D (
('t', 'u', 'p', 'l', 'e')) — this is the tuple BEFORE concatenation with t2; it ignores thet += t2step entirely.
Exam trap
- The single trailing comma in
t[2],is easy to miss but is essential — without it,t2would just be the string'p', andt += t2would raise a TypeError (cannot concatenate tuple with str) rather than work as shown.
Common Mistakes
- 1Missing the trailing comma in `t[2],` and assuming t2 is just the character 'p' rather than a 1-element tuple.
- 2Forgetting that tuple() applied to a string splits it into individual characters, not treating the whole string as one element.
Interesting Facts
A single-element tuple MUST have a trailing comma — (5) is just the integer 5 in parentheses, but (5,) is a genuine tuple.
Tuples support the += operator for concatenation just like lists, but unlike lists, this creates a brand-new tuple object each time since tuples are immutable.
Spotted a mistake or something unclear?
Tell us — we fix reported answers fast.
Frequently Asked Questions
Why does a trailing comma matter when creating a tuple?
In Python, it is the comma — not the parentheses — that actually defines a tuple. `(value,)` with a trailing comma creates a genuine 1-element tuple, while `(value)` without a comma is just the value itself inside parentheses.
What does tuple('tuple') return?
It converts the string into a tuple of its individual characters: ('t', 'u', 'p', 'l', 'e'), since strings are iterable sequences of characters.