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.
Output:
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.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer