Open In App

Python – Concatenation of two String Tuples

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with records, we can have a problem in which we may need to perform String concatenation of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using zip() + generator expression The combination of above functions can be used to perform this task. In this, we perform the task of String concatenation using generator expression and mapping index of each tuple is done by zip(). 

Python3




# Python3 code to demonstrate working of
# Tuple String concatenation
# using zip() + generator expression
 
# initialize tuples
test_tup1 = ("Manjeet", "Nikhil", "Akshat")
test_tup2 = (" Singh", " Meherwal", " Garg")
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple String concatenation
# using zip() + generator expression
res = tuple(ele1 + ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
 
# printing result
print("The concatenated tuple : " + str(res))


Output : 

The original tuple 1 : ('Manjeet', 'Nikhil', 'Akshat')
The original tuple 2 : (' Singh', ' Meherwal', ' Garg')
The concatenated tuple : ('Manjeet Singh', 'Nikhil Meherwal', 'Akshat Garg')

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 : Using map() + concat The combination of above functionalities can also perform this task. In this, we perform the task of extending logic of concatenation using concat and mapping is done by map(). 

Python3




# Python3 code to demonstrate working of
# Tuple String concatenation
# using map() + concat
from operator import concat
 
# initialize tuples
test_tup1 = ("Manjeet", "Nikhil", "Akshat")
test_tup2 = (" Singh", " Meherwal", " Garg")
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple String concatenation
# using map() + concat
res = tuple(map(concat, test_tup1, test_tup2))
 
# printing result
print("The concatenated tuple : " + str(res))


Output : 

The original tuple 1 : ('Manjeet', 'Nikhil', 'Akshat')
The original tuple 2 : (' Singh', ' Meherwal', ' Garg')
The concatenated tuple : ('Manjeet Singh', 'Nikhil Meherwal', 'Akshat Garg')

Method #3: Use a for loop to iterate over the indices of the tuples and concatenate the corresponding elements.

Step-by-step approach:

  • Create an empty tuple named res to store the concatenated strings.
  • Use a for loop with the range() function and the len() function to iterate over the indices of test_tup1.
  • Inside the loop, concatenate the corresponding elements of test_tup1 and test_tup2 using the + operator and add the result to res as a tuple using the += operator.
  • Print the concatenated tuple using the print() function and the concatenation operator +.
  • The program is complete.

Below is the implementation of the above approach:

Python3




# initialize tuples
test_tup1 = ("Manjeet", "Nikhil", "Akshat")
test_tup2 = (" Singh", " Meherwal", " Garg")
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple String concatenation using a for loop
res = ()
for i in range(len(test_tup1)):
    res += (test_tup1[i] + test_tup2[i],)
 
# printing result
print("The concatenated tuple : " + str(res))


Output

The original tuple 1 : ('Manjeet', 'Nikhil', 'Akshat')
The original tuple 2 : (' Singh', ' Meherwal', ' Garg')
The concatenated tuple : ('Manjeet Singh', 'Nikhil Meherwal', 'Akshat Garg')

Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n) to store the result tuple.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads