Hello geeks and welcome in today’s article, we will cover Tuple to String In Python. We will look at different methods with the help of which we can perform this operation. Along with that, we will also look at various examples. We know that Tuple stores multiple items in a single variable. Moreover, it is an ordered and unchangeable collection of data. Our goal here is to convert it into a String, which is a sequence of characters. The characters can be alphabets, some symbols, or even numbers enclosed between double inverted commas. Moving ahead, we will look at different methods that are used to perform this operation.
1. STR.JOIN() FOR TUPLE TO STRING IN PYTHON
So in our first method, we use function str.join(), also called the join function. This function returns a string by joining all the different elements of an iterable separated by a string separator. It is a flexible method that not only works with Tuples but with lists also.
The general syntax for the above method is
string.join(iterable)
It has a straightforward syntax with not many parameters associated. It accepts any iterable value that is capable of returning one element at a time. Now let us look at one example that will further help us in understanding the concept.
#tuple declration nameTuple = ('P','Y','T','H','O','N', 'P','O','O','L') separator="" print(separator.join(nameTuple))
Output:
PYTHONPOOL
Here in the above example, we can see that we have first declared a Tuple. In the next line declared, we have specified a string separator. Finally, we have written our print statement as per the syntax, and we get our desired output. Here in the separator, we can also use “,” but in that case, our output will be “P, Y, T, H, O, N, P, O, L, L.”
2.REDUCE() METHOD
The reduce() function is defined in the functools module of Python. It receives two arguments, a function and an iterable but returns only a single value. Now let us look at its example that will help us in better understanding of this method.
import functools import operator def convertTuple(tup): str = functools.reduce(operator.add, (tup)) return str name = ('P', 'Y', 'T', 'H', 'O','N','P','O','O','L') str = convertTuple(name) print(str)
Output:
PYTHONPOOL
In the above example, at first, we have imported the functools module. In the next, we have imported the operator that is responsible for performing the basic athematic operations. Then we have used the reduce method, after which we have defined a Tuple. Following this, we have ConvertTuple as defined earlier to print a string.
3.FOR LOOP METHOD FOR TUPLE TO STRING IN PYTHON
Next up, we will be looking at how to use a loop. We can convert Tuples to String. We are going to use a for loop to achieve our objective. Let us look at a sample problem that will help us understand the concept in a better way.
name = ('P', 'Y', 'T', 'H', 'O', 'N','P','O','O','L') str = '' for item in name: str = str + item print(str)
Output:
PYTHONPOOL
In the above problem, we have at first defined a Tuple. Following which we have used the “str” that converts a specific value to a string. Up next, we have used For loop and at last printed our “str.”
Must Read
- Python Help Function | How to make use of Python Help
- HOW TO PYTHON SORT LIST OF TUPLES
- Python sum | python sum list | sum() function in Python
CONCLUSION
In this article, we covered Tuple to String in Python. We looked at various methods through which we can perform this conversion. To be STR.JOIN(), REDUCE(), and FOR LOOP are the three methods we covered in this article. I hope this article was able to clear all your queries. In case you have any unsolved doubts, please feel free to write them below in the comment section. Done reading this, why not read the sort list of tuples next.
The post METHODS TO CONVERT TUPLE TO STRING IN PYTHON appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment