Concatenate tuple elements in Python
Hello programmers, in this tutorial, we will learn how to concatenate tuple elements in Python.
The tuple is immutable but we can concatenate two tuples using the sum() function and it will concatenate the values of the tuples.
Sum() function to add multiple tuples into a single tuple
sum() function adds two or more tuples elements into a single tuple which contains all the elements of those tuples.
Coding:
- we have to create two tuples t1 and t2, with some elements in them.
- Now we just use the sum() function and pass these two tuples in it and we assign this to a new list concate_tuple.
- At last, we print our new concatenated tuple.
#tuple t1 t1=(1,2,3,4) #tuple t2 t2=(5,6,7,8) #concatenated tuple concate_tuple=sum((t1,t2),()) print("concatenated tuple: ",concate_tuple)
output:
concatenated tuple: (1, 2, 3, 4, 5, 6, 7, 8)
Hopefully, you have learned how to concatenate tuple elements in Python.
Also read: How to remove the last element from a tuple in Python
Leave a Reply