Python program to concatenate tuples to make a nested tuple

By IncludeHelp Last updated : November 26, 2023

Problem statement

Given a tuple, write a Python program to concatenate tuples to make a nested tuple.

Solution

To concatenate tuples to make a nested tuple, use the plus (+) operator and the comma (,) operator after tuples. Let's suppose there are two tuples tpl1 and tpl2, then the statement (tpl1,) + (tpl2,) will create a nested tuple.

Example

Python program to concatenate tuples to make a nested tuple.

# Code to concatenate tuples to make a nested tuple
 
# Creating two tuples
tpl1 = (1, 2, 3)
tpl2 = (20, 30, 10)
 
# Printing the original tuples
print("Tuple 1:", tpl1)
print("Tuple 2:", tpl2)
 
# Concatenating the tuples
result = (tpl1,) + (tpl2,)
 
# Printing result i.e., nested tuple
print("The nested tuple is:", result)

Output

The output of the above example is:

Tuple 1: (1, 2, 3)
Tuple 2: (20, 30, 10)
The nested tuple is: ((1, 2, 3), (20, 30, 10))

Python Tuple Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.