I have a tuple:
a = (1,2,3)
and I need to add a tuple at the end
b = (4,5)
The result should be:
(1,2,3,(4,5))
Even if I wrap b in extra parents: a + (b), I get (1,2,3,4,5) which is not what I wanted.
tuple objects are immutable. The result you're getting is a result of the fact that the + (and +=) operator is overridden to allow "extending" tuples the same way as lists. So when you add two tuples, Python assumes that you want to concatenate their contents.
To add an entire tuple onto the end of another tuple, wrap the tuple to be added inside another tuple.
c = a + (b,) # Add a 1-tuple containing the tuple to be added.
print(c) # >>> (1, 2, 3, (4, 5))
a + (b,). Otherwise Python will consider(b)as simplybwrapped in brackets. The comma makes it a tuple.