- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I convert a Python tuple to string?
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. There are three different ways we can convert a python tuple to string.
Using a for loop.
Using the python join() method
Using the functool.reduce() method
Using the for loop
In python, we can easily iterate the tuple elements using the for loop, then we will append/add each element to the string object. In this below example we will see how to convert the tuple to string.
Example
To avoid the TypeError while concatenating, we have changed the type of loop element before adding to the string.
t = ('p', 'y', 't', 'h', 'o', 'n', ' ', 3, '.', 10, '.', 0 ) print("Input tuple: ", t) print(type(t)) s = '' # crete en empty string for ele in t: s += str(ele) print("String Output: ", s) print(type(s))
Output
Input tuple: ('p', 'y', 't', 'h', 'o', 'n', ' ', 3, '.', 10, '.', 0) <class 'tuple'> String Output: python 3.10.0 <class 'str'>
Using the python join() method
To convert a Python tuple to a string, we will use join() method. The join() is a python string method that takes an iterable object like tuple as its argument and returns a Python string joined using a string separator or delimiter.
Syntax
str.join(iterable)
Example
Let’s take an example and convert a python tuple to string.
t = ('p', 'y', 't', 'h', 'o', 'n' ) print("Input tuple: ", t) print(type(t)) output = "".join(t) print("String Output: ", output) print(type(output))
Output
Input tuple: ('p', 'y', 't', 'h', 'o', 'n') <class 'tuple'> String Output: python <class 'str'>
Example
The join() method will raise a TypeError, if we apply the join() method to a tuple that contains mixed data types (string, float, and integer). To avoid this error, we need to convert all tuple elements to the string data type.
t = ('p', 'y', 't', 'h', 'o', 'n', ' ', 3.10, '.', 0 ) print("Input tuple: ", t) print(type(t)) output = "".join(map(str,t)) print("String Output: ", output) print(type(output))
Output
Input tuple: ('p', 'y', 't', 'h', 'o', 'n', ' ', 3.1, '.', 0) <class 'tuple'> String Output: python 3.1.0 <class 'str'>
By using the map() function we first converted the all tuple elements to the string data type. Then it is passed to the join method.
Using the functool.reduce() method
The reduce() function is available in the functool module and it takes a function as its first argument and an iterable as its second argument.
Syntax
functools.reduce(function, iterable[, initializer])
Example
import functools import operator t = ('p', 'y', 't', 'h', 'o', 'n' ) print("Input tuple: ", t) print(type(t)) output = functools.reduce(operator.add, t) print("String Output: ", output) print(type(output))
Output
Input tuple: ('p', 'y', 't', 'h', 'o', 'n') class 'tuple'> String Output: python <class 'str'>
We have to import two Python modules funtools and operator to use the reduce() and the add() functions, for converting the tuple to a string.