Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Program to convert a tuple to a string

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 : Lazyroar

Approach 1: using simple for loop

Create an empty string and using a for loop iterate through the elements of the tuple and keep on adding each element to the empty string. In this way, the tuple is converted to a string. It is one of the simplest and the easiest approaches to convert a tuple to a string in Python.

Python3




# Python3 code to convert a tuple
# into a string using a for loop
 
 
def convertTuple(tup):
        # initialize an empty string
    str = ''
    for item in tup:
        str = str + item
    return str
 
 
# Driver code
tuple = ('g', 'e', 'e', 'k', 's')
str = convertTuple(tuple)
print(str)


Output:

Lazyroar

Time complexity: O(n), where n is the length of the input tuple. This is because the program iterates through the input tuple exactly once, performing constant-time operations on each element.
Auxiliary space: O(n), where n is the length of the input tuple. This is because the program creates a string to store the resulting string, which can have a maximum length of n. 

Approach 2: using str.join() 

The join() method is a string method and returns a string in which the elements of the sequence have been joined by a str separator. 
Using join() we add the characters of the tuple and convert it into a string. 
 

Python3




# Python3 code to convert a tuple
# into a string using str.join() method
 
 
def convertTuple(tup):
    str = ''.join(tup)
    return str
 
 
# Driver code
tuple = ('g', 'e', 'e', 'k', 's')
str = convertTuple(tuple)
print(str)


Output:

Lazyroar

Time complexity: O(n), where n is the length of the input tuple. The reason being, the join() method has a time complexity of O(n), where n is the length of the input tuple, as it concatenates all the elements in the tuple.
Auxiliary space: O(n), where n is the length of the input tuple. The reason being, a new string is created to store the concatenated elements in the tuple. However, since the string is created outside the scope of the function and returned, the space complexity is not dependent on the input size and can be considered constant.

Approach 3: using  str.join() function and map() function

The str.join() function works well when we have only string objects as the tuple elements but if the tuple contains at least one non-string object then the str.join() function will fail and show a TypeError as a string object cannot be added to a non-string object. Hence to avoid this TypeError we will make use of map() function inside the join(). This map() function will take the iterator of the tuple and str() function as its parameters and convert each element of the tuple into a string.

Python3




# Python3 code to convert a tuple
# into a string using str.join() & map() functions
 
 
def convertTuple(tup):
    st = ''.join(map(str, tup))
    return st
 
 
# Driver code
tuple = ('g', 'e', 'e', 'k', 's', 101)
str = convertTuple(tuple)
print(str)


Output:

Lazyroar101

Approach 4: 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 a tuple. 
 

Python3




# Python3 code to convert a tuple
# into a string using reduce() function
import functools
import operator
 
 
def convertTuple(tup):
    str = functools.reduce(operator.add, (tup))
    return str
 
 
# Driver code
tuple = ('g', 'e', 'e', 'k', 's')
str = convertTuple(tuple)
print(str)


Output:

Lazyroar

Method 5: Use a list comprehension + join()

Uses a list comprehension instead of a generator expression. The resulting list of strings is passed as an argument to the join() method to concatenate the strings. The resulting string is returned by the convertTuple() function.

Python3




def convertTuple(tup):
    return ''.join([str(x) for x in tup])
 
# Driver code
tuple = ('g', 'e', 'e', 'k', 's')
str = convertTuple(tuple)
print(str)


Output

Lazyroar

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

RELATED ARTICLES

Most Popular

Recent Comments