Ways to concatenate tuples in Python


When it is required to concatenate multiple tuples, the '+' operator can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.

The '+' operator can be used to add numeric values or concatenate strings.

Below is a demonstration of the same −

Example

Live Demo

my_tuple_1 = (11, 14, 0, 78, 33, 11)
my_tuple_2 = (10, 78, 0, 56, 8, 34)

print("The first tuple is : ")
print(my_tuple_1)
print("The second tuple is : ")
print(my_tuple_2)

my_result = my_tuple_1 + my_tuple_2

print("The tuple after concatenation is : " )
print(my_result)

Output

The first tuple is :
(11, 14, 0, 78, 33, 11)
The second tuple is :
(10, 78, 0, 56, 8, 34)
The tuple after concatenation is :
(11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34)

Explanation

  • Two tuples are defined and are displayed on the console.
  • They are concatenated using the '+' operator.
  • This is assigned to a value.
  • It is displayed on the console.

Updated on: 12-Mar-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements