Python | Program to convert a tuple to a string
Given a tuple of characters, Write a python program to convert the tuple into a string.
Examples:
Input : ('a', 'b', 'c', 'd', 'e')
Output : abcde
Input : ('g', 'e', 'e', 'k', 's')
Output : geeks
Approach
There are various approaches to convert a tuple to a string.
- Approach 1 : using str.join()
The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator.
Using join() we add the characters of the tuple and convert it into string.# Python3 code to convert tuple# into stringdefconvertTuple(tup):str=''.join(tup)returnstr# Driver codetuple=('g','e','e','k','s')str=convertTuple(tuple)print(str)chevron_rightfilter_noneOutput:geeks
- Approach 2 : using reduce() function
The reduce(fun, seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along. We pass the add operator in the function to concatenate the characters of tuple.# Python3 code to convert tuple# into stringimportfunctoolsimportoperatordefconvertTuple(tup):str=functools.reduce(operator.add, (tup))returnstr# Driver codetuple=('g','e','e','k','s')str=convertTuple(tuple)print(str)chevron_rightfilter_noneOutput:geeks
- Python | Convert a list into a tuple
- Python | Convert a list into tuple of lists
- Python program to convert hex string to decimal
- Python | Program to convert String to a List
- Python program to convert a list to string
- Python program to convert camel case string to snake case
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert None to empty string
- Python | Convert String to Binary
- Python | Convert String to bytes
- Python | Convert a list of characters into a string
- Python | Convert Bytearray to Hexadecimal String
- Python | Convert dictionary object into string
- Python program to convert POS to SOP
- Python | Convert string to DateTime and vice-versa
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



