×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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 »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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