ADVERTISEMENT
ADVERTISEMENT

Python program to concatenate tuple elements by delimiter

Here, we have a list of tuples and we need to concatenate elements of tuple using a delimiter.
Submitted by Shivang Yadav, on September 06, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Concatenating Tuple elements by Delimiter

We have a tuple consisting of elements and a delimiter string. And we need to concatenate the values to a string.

Input:
tuple = ("Python", "program", 5, "scala"), delimiter = “-”

Output:
Python-program-5-scala

For performing this task, we have more than one method to solve the problem.

Method 1:

To concatenate the tuples, we will use the str() to convert all characters to string and then concatenate them with a delimiter using join() method.

The map is used to perform the mapping of the string.

# Python program to concatenate tuples 
# using delimiter 

# Creating and printing the tuple and delimiter
myTuple = ("Python", "program", 5, "scala")
print("Tuple values are " + str(myTuple))
delimiter = "-"

# Concatenating the tuple using delimiter 
concatenated = delimiter.join(map(str, myTuple))

# printing Concatenated string 
print("Concatenated Tuple with delimiter : " + str(concatenated))

Output:

Tuple values are ('Python', 'program', 5, 'scala')
Concatenated Tuple with delimiter : Python-program-5-scala

Method 2:

Another method to solve the problem is by using list comprehension where we will loop through the tuple and using the + operator add a delimiter to the string.

# Python program to concatenate tuples 
# using delimiter 

# Creating and printing the tuple and delimiter
myTuple = ("Python", "program", 5, "scala")
print("Tuple values are " + str(myTuple))
delimiter = "-"

# Concatenating the tuple using delimiter 
concatenated = ''.join([str(ele) + delimiter for ele in myTuple])

# Logic for removing the ending delimiter...
concatenated = concatenated[ : len(concatenated) - len(delimiter)]

# Printing Concatenated string 
print("Concatenated Tuple with delimiter : " + str(concatenated))

Output:

Tuple values are ('Python', 'program', 5, 'scala')
Concatenated Tuple with delimiter : Python-program-5-scala

Python Tuple Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.