In python, a tuple can contain different elements. On the other hand, a string is just a sequence of characters. In this article, we will discuss how to convert a tuple to a string when we are given a tuple of numbers of characters in python.
How to Convert Tuple of Characters to a String?
Suppose that we have the following tuple of characters.
myTuple = ("P", "y", "t", "h", "o", "n")
Now, we have to convert this tuple to the string “Python”
.
To convert the tuple to string, we will first create an empty string named output_string
. After that, We will traverse the tuple using a for loop. While traversal, we will add each character in the tuple to the output_string
using the string concatenation operation. After execution of the for loop, we will get the desired string in the variable output_string
. You can observe this in the following example.
myTuple = ("P", "y", "t", "h", "o", "n")
output_string = ""
for character in myTuple:
output_string += character
print("The input tuple is:", myTuple)
print("The output string is:", output_string)
Output:
The input tuple is: ('P', 'y', 't', 'h', 'o', 'n')
The output string is: Python
Instead of using the for loop, we can use the join()
method to convert the tuple of characters into a string. The join()
method, when invoked on a string, takes an iterable object containing strings as an input argument. After execution, it returns the modified string containing the new character along with the characters in the original string.
To convert a tuple of characters to a string, we will first create an empty string named output_string
. After that, we will invoke the join()
method on the output_string
with the tuple as input argument. After execution, the join()
method will return the desired string as shown below.
myTuple = ("P", "y", "t", "h", "o", "n")
output_string = ""
output_string = output_string.join(myTuple)
print("The input tuple is:", myTuple)
print("The output string is:", output_string)
Output:
The input tuple is: ('P', 'y', 't', 'h', 'o', 'n')
The output string is: Python
Convert Tuple of Numbers to a String
If you try to convert a tuple of numbers to a string using any of the methods discussed above, the program will raise TypeError
exception. You can observe this in the following example.
myTuple = (1, 2, 3, 4, 5, 6)
output_string = ""
for character in myTuple:
output_string += character
print("The input tuple is:", myTuple)
print("The output string is:", output_string)
Output:
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module>
output_string += character
TypeError: can only concatenate str (not "int") to str
Similarly, when we use the second approach, the program runs into TypeError
exception as shown below.
myTuple = (1, 2, 3, 4, 5, 6)
output_string = ""
output_string = output_string.join(myTuple)
print("The input tuple is:", myTuple)
print("The output string is:", output_string)
Output:
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 3, in <module>
output_string = output_string.join(myTuple)
TypeError: sequence item 0: expected str instance, int found
To avoid the error, we just have to convert each element of the tuple into a string before adding it to the output_string
. In the first approach, we will first convert each element of the tuple into a string using the str()
function. After that, we will perform the concatenation operation. In this way, we can convert a tuple of numbers to a string.
myTuple = (1, 2, 3, 4, 5, 6)
output_string = ""
for element in myTuple:
character = str(element)
output_string += character
print("The input tuple is:", myTuple)
print("The output string is:", output_string)
Output:
The input tuple is: (1, 2, 3, 4, 5, 6)
The output string is: 123456
For the approach using the join()
method, we will first convert the tuple of numbers into a tuple of strings. For this, we will use the map()
function and the str()
function.
The map()
function takes a function as its first argument and an iterable object as the second input argument. After execution, it returns a map object in which the function is applied to each element of the iterable object. We will pass the str()
function and the tuple of numbers to the map()
function as input arguments. After that, we will convert the map object using the tuple()
constructor. Hence, we will get a tuple of strings as shown below.
myTuple = (1, 2, 3, 4, 5, 6)
newTuple = tuple(map(str, myTuple))
print("The input tuple is:", myTuple)
print("The output tuple is:", newTuple)
Output:
The input tuple is: (1, 2, 3, 4, 5, 6)
The output tuple is: ('1', '2', '3', '4', '5', '6')
After getting the tuple of strings from the tuple of numbers, we can directly obtain the string as follows.
myTuple = (1, 2, 3, 4, 5, 6)
newTuple = tuple(map(str, myTuple))
output_string = ''.join(newTuple)
print("The input tuple is:", myTuple)
print("The output string is:", output_string)
Output:
The input tuple is: (1, 2, 3, 4, 5, 6)
The output string is: 123456
Conclusion
In this article, we have discussed how to convert a tuple to a string in python. To know more about strings in python, you can read this article on string formatting in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.