Skip to content
Related Articles

Related Articles

Python – Concatenate Tuple elements by delimiter

Improve Article
Save Article
  • Last Updated : 02 Sep, 2020
Improve Article
Save Article

Given a tuple, concatenate each element of tuple by delimiter.

Input : test_tup = (“Gfg”, “is”, 4, “Best”), delim = “, ”
Output : Gfg, is, 4, Best
Explanation : Elements joined by “, “.

Input : test_tup = (“Gfg”, “is”, 4), delim = “, ”
Output : Gfg, is, 4
Explanation : Elements joined by “, “.

Method #1 : Using list comprehension

In this, we iterate for each element in tuple using loop in list comprehension and concatenate using + operator.

Python3




# Python3 code to demonstrate working of 
# Concatenate Tuple elements by delimiter
# Using list comprehension
  
# initializing tuple
test_tup = ("Gfg", "is", 4, "Best")
  
# printing original tuple
print("The original tuple is : " + str(test_tup))
  
# initializing delim 
delim = "-"
  
# using str() to convert elements to string 
# join to convert to string
res = ''.join([str(ele) + delim for ele in test_tup])
  
# striping stray char 
res = res[ : len(res) - len(delim)]
  
# printing result 
print("Concatenated Tuple : " + str(res)) 


Output

The original tuple is : ('Gfg', 'is', 4, 'Best')
Concatenated Tuple : Gfg-is-4-Best

Method #2 : Using join() + map()

In this, we convert all chars to string using str() and map to perform str() to all elements, then concatenation using join().

Python3




# Python3 code to demonstrate working of 
# Concatenate Tuple elements by delimiter
# Using join() + map()
  
# initializing tuple
test_tup = ("Gfg", "is", 4, "Best")
  
# printing original tuple
print("The original tuple is : " + str(test_tup))
  
# initializing delim 
delim = "-"
  
# for joining, delim is used 
res = delim.join(map(str, test_tup))
  
# printing result 
print("Concatenated Tuple : " + str(res)) 


Output

The original tuple is : ('Gfg', 'is', 4, 'Best')
Concatenated Tuple : Gfg-is-4-Best

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!