Example:
tuple = ("python", "includehelp", 43, 54.23)
Concatenating tuples to nested tuple
We are given multiple tuples consisting of integer elements. We need to create a Python program to concatenate the tuples into a single nested tuple.
Input:
(5, 6, 1, 8)
(7, 2, 9, 3)
Output:
((5, 6, 1, 8), (7, 2, 9, 3))
Method 1:
One method to perform the task is by using the addition operator "+" on the tuples which will add them both creating a new tuple consisting of both the tuple. Generally, the method creates a tuple with no nesting but we will add ',' to each tuple while passing this will return a nested tuple.
Output:
Method 2:
Another method to concatenate the tuples is by using the sum function. While passing we will be passing the tuples in a different way. This will create the new tuple with tuples as elements.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer