In this post, we are going to learn about different ways of how to Convert Tuple to string in Python to string with code examples. Here we will be learning with examples like Python Tuple to string with a comma, Python Tuple to string with space, Convert int tuple to string Python, Python tuple of date to a string, Python tuple of byte to string, Python list of numeric tuples to list of string.
1.Convert Tuple to string in Python with comma
In this example, we are converting a tuple to a string with a delimiter comma(,) by using the string join() method and print the result using the print(tuple_to_str) statement.
Program Example
#Python3 program to convert Tuple to string with comma
tuple_fruit = ('apple','bana','gauva','grapes')
tuple_to_str = ','.join(tuple_fruit)
print(tuple_to_str)
Output
apple,bana,gauva,grapes
2. Python tuple to string with space
In this example, we are converting tuple to string with delimiter space( ). using list comprehension with the join() method.
Program Example
#python 3 program to convert Python tuple to string with space
tuple_fruit = ('apple','bana','gauva','grapes')
tuple_to_str = " ".join([str(item) for item in tuple_fruit])
print(tuple_to_str)
Output
apple bana gauva grapes
3. Convert int tuple to string in Python
In this example, we are using List comprehension with string join() method to convert a tuple of int to string tuple with delimiter comma. We can use any delimiter as per need.
Program to Convert int tuple to string
tuple_fruit = (21,32,46,50)
tuple_to_str = ", ".join([str(item) for item in tuple_fruit])
print(tuple_to_str)
Output
21, 32, 46, 50
4.Convert tuple of numbers to string with Map() and join()
In this example, we are converting a tuple of numbers to a string using string.join() along with map() using delimiter space( ).
#python3 program to Convert int tuple to string Python
tuple_fruit = (21,32,46,50)
tuple_to_str = ' '.join(map(str, tuple_fruit))
print(tuple_to_str)
Output
21 32 46 50
5. Convert tuple of date to string in Python
In this example, we will convert a tuple of dates to a string. For manipulation of date and time, we will need to import the datetime module.
- Using datetime class’s date method to convert tuples of date to string
- printing the result using print(dt_obj.ctime())
Program Example
#python 3 Program to convert tuple of date to string
import datetime
date_tuple = (8,18, 2021)
dt_obj = datetime.date(month=date_tuple[0], day=date_tuple[1], year=date_tuple[2])
print(dt_obj.ctime())
print(type(dt_obj.ctime()))
Output
Wed Aug 18 00:00:00 2021
<class 'str'>
6. Python tuple of byte to string
To convert tuple of byte to string in Python. We are using the Python byte() function.
Python Program Example
#python3 program to convert Python tuple of byte to string
tuple_of_byte = bytes(tuple([0, 6, 8, 10,18]))
Output
b'\x00\x06\x08\n\x12'
7. Convert list of numeric tuples to list of string in Python
In this example, we are converting a Python list of numeric tuples to a list of strings using map() with a list comprehension.
Program Example
Python3 Program to List of tuples to list of string tuple
list_of_tuples = [(8,18), (3,21),(9,8),(20,11)]
print([tuple(map(str, item)) for item in list_of_tuples])
Output
[('8', '18'), ('3', '21'), ('9', '8'), ('20', '11')]
8. Convert list of Tuple to list of string
To convert a list of tuples to a list of strings we can use the map() function along with the join() function
Program Example
# Python3 Program to convert list of tuple to list of list string
original_list = [('D', 'E', 'v', 'e', 'n','u','m'), ('I', 'T'),
('S', 'I', 'T', 'E')]
print ("The original list is : " + str(original_list))
# map() + join() converting f list of tuple to list of list
res = list(map(''.join, original_list))
print ("\n The list of string : " + str(res))
Output
The original list is : [('D', 'E', 'v', 'e', 'n', 'u', 'm'), ('I', 'T'), ('S', 'I', 'T', 'E')]
The list of string : ['DEvenum', 'IT', 'SITE']
Summary
In this post, we have learned different ways of How to convert Python Tuple to string with a code example that includes Python Tuple to string with a comma, Python Tuple to string with space, Convert int tuple to string Python, Python tuple of date to a string, Python tuple of byte to string, Python list of numeric tuples to list of string