Example:
tuple = ("python", "includehelp", 43, 54.23)
Concatenation of two string tuples
In this article, we are given two tuples with string elements. We will be creating a python program to perform concatenation of two string tuples.
Input:
tup1 = ('python', 'learn', 'web')
tup2 = ('programming', 'coding', 'development')
Output:
('python programming', 'learn coding', 'web development')
One method to solve the problem is by iterating over the tuples and concatenating the string.
Method 1:
One method to solve the problem is by using the + operator to concatenate the strings. And use zip along with the generator expression to create a new tuple with string concatenated.
Output:
The elements of tuple 1 : ('python', 'learn', 'web') The elements of tuple 2 : (' programming', ' coding', ' development') The tuple with concatenated string : ('python programming', 'learn coding', 'web development')Method 2:
Another method to solve the problem is by using concat method to concatenate string values from tuples. Then map these values then convert them to the tuple.
The concat method is imported from the operator library.
Output:
The elements of tuple 1 : ('python', 'learn', 'web') The elements of tuple 2 : (' programming', ' coding', ' development') The tuple with concatenated string : ('python programming', 'learn coding', 'web development')need an explanation for this answer? contact us directly to get an explanation for this answer