In this article, we are going to learn everything about how to convert a tuple to a string in Python with the different-2 examples. As a developer, sometimes you need to convert a tuple to a string in Python, Then you can use anyone from this which will be described in this article. Before going through this article, we will discuss a little bit about Python tuple and string.
Headings of Contents
What is Python Tuple ?
Python tuple is the core data structure in Python which stores collections of items in a single variable. Python tuple is ordered, immutable or unchangeable, and also it allows duplicate items.
What is String in Python ?
In Python, the string is a collection of Unicode characters encoded inside single, double, or triple quotes. Actually, string in Python is immutable that means you can not change value string once it is created.
Now it’s time to convert python tuple to string.
How to Convert a Tuple to a String in Python ?
Here we are going to see multiple ways to convert Python tuple to string with the help of an example. These all are the best ways to convert any Python tuple to string.
- Using for loop
- Using reduce() function
- Using join() function
- Using map() and join() function
- Using list comprehension
Using for loop
First, we will create an empty string and then using for loop through iterate each of the items of the tuple and keep on adding to the empty string, Through this technique, you can convert any Python tuple to the string. This is one of the best ways to convert python tuple to string without using any built-in functions.
Example: Python program to convert a tuple to string
# tuple
tpl = ('P', 'y', 't', 'h', 'o', 'n')
# empty string
string = ''
for i in tpl:
string = string + i
print("The String is: ", string)
Output
The string is: Python
Python for loop:- Click Here
Using reduce() function
Here we will use reduce()
function to convert a tuple to a string. reduce()
function takes two arguments first is function and the second one is iterable and the function will apply each of the items of the iterable. To use the reduce() function you have to import it from the functools
module using the import
keyword.
Example: Python program to convert a tuple to string
from functools import reduce
tpl = ('P', 'y', 't', 'h', 'o', 'n')
result = reduce(lambda a, b: a + b, tpl)
print("The Converted string is:- ", result)
Output
The Converted string is:- Python
Python reduce() function:- Click Here
Using join() function
The join()
method is a string method in Python that takes iterable as a parameter and joins all the items of the iterable with a string separator.
Example: Python program to convert a tuple to string
tpl = ('P', 'y', 't', 'h', 'o', 'n')
result = ''.join(tpl)
print("The Converted string is:- ", result)
Output
The Converted string is:- Python
Using map() and join() function
Suppose you have a tuple that contains an integer and you want to convert it into a string, then you will get an error. As you can see in the below example.
Example
tpl = (1, 2, 3, 4, 5)
# empty string
string = ''
for i in tpl:
string = string + i
Read join() function:- Click Here
Output
Traceback (most recent call last):
File "/home/sazid/Vishvajit/flask-app/test.py", line 8, in <module>
string = string + i
TypeError: can only concatenate str (not "int") to str
To fix the above problem, we will use the map()
function because it converts all the items of the tuple into a string.
Let see how can we do that.
Example
tpl = (1, 2, 3, 4, 5)
# empty string
string = ''
for i in map(str, tpl):
string = string + i
print("String is: ", string)
Output
The String is: 12345
Read map() function:- Click Here
Using list comprehension
List comprehension is the concise way to convert a tuple to a string.
Example
tpl = (1, 2, 3, 4, 5)
result = ''.join([str(i) for i in tpl])
print("The string is:- ", result)
Conclusion
So, in this guide, we have seen various ways to convert a tuple to a string in Python with the help of proper examples. You can use any one of them to convert your Python tuple to string. List comprehension is one of the most effective ways to convert any Python tuple to string because it fast rather than others.
I hope this article is helpful for you. If you like this article, please share, support us and keep visit for further Python interesting tutorials.
Thanks for reading ….