Python

Python Tuple to String

In this tutorial, we will explain how to convert Tuple to Python string. We will deliberate different approaches with the help of the Spyder compiler by implementing various example codes. We all know that a Tuple saves various elements in a single variable. Also, it is a well-ordered and fixed collection of data. Python tuples are unchallengeable. It means that you can’t alter the tuple elements. For instance, we use a tuple to save a list of student names.

Now, what we do is to change it into a Python string, which is an order of characters. The characters include symbols, alphabets, or also numbers encircled between double-quotes. To proceed further, we will look at various strategies that can be used to perform this procedure.

Example 1

So in our first example code, we use the str.join() function, also known as join function. It returns a string by connecting all the distinct items of a traversal parted by a string separator. It is a handy approach that not just works with Tuples but also handles Python lists. It does not accept too many parameters but only takes any iterable item that returns one item at a time. Come, let’s move to the example that helps you to understand the whole operation.

We use a new file to describe the working of the join() method. Here in the below illustration, we can view that we have first declared and initialized a Tuple. The elements in the tuple can be amended depending on your work needs. In our next statement, we have created a string separated by a comma. Then we declare a delimiter. At last, we use a print statement that takes a separator.join() function as a parameter and displays the desired outcome.

tuple_1 = (‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’)
separator = “”
print(separator.join(Tuple_1)

Simply save the program file by clicking Ctrl+S and then hit the F5 button on the keyboard to run the code file.

Example 2

In our second example code, we use reduce() function which is stated in the Python functools module. It takes two parameters, an iterator or a function but yields only a single element. Let’s proceed further to better understand the method.

We use the same program file to explain the working of reduce() method. Here in the below illustration, we first import the functools and operator module. The operator module executes the basic mathematical operations. We can then call the reduce() method that takes two arguments, and then we have created a Tuple. The elements in the tuple can be amended depending on your work needs. In this program, we have defined a ConvertTuple that prints a string. After this, we use a print statement that demonstrates the subsequent output on the screen.

import functools
import operator

def convertTuple(tup)
    str_1 = functools.reduce(operator.add, (tup))
    return str_1

tuple_1 = (‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’)
str_1 = convertTuple(Tuple_1)
print(str_1)

Simply save the program file and then hit the F5 button on the keyboard to run the code file.

Example 3

In our third example code, we use the loop method to convert tuples to python strings. We use for loop to attain our goal. Let’s have a look at a sample code that helps you to understand the operation efficiently. In this sample code, we have first generated a Tuple. The elements in the tuple can be amended depending on your work needs. We can declare “str” that alters a specific item to a string. Then we used a loop that traverses over the tuple. In the end, we use the print function to display the converted string.

tuple_1 = (‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’)
str = ‘’

for item in Tuple_1:
    str = str + item

print(str)

Again, save and execute the program code to show the converted string on the console screen.

Conclusion

In this tutorial, we discussed different ways to convert Tuple to Python String. These example codes can help you to implement this conversion. We have covered STR.JOIN(), REDUCE(), and FOR LOOP methods. I hope this guide was able to answer all your queries. STR.JOIN() and LOOP methods are the best practice methods. You don’t need to use multiple lines of code just like you did in the reduce() method.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content