Tuple to String in PythonWe know that lists, tuples, and dictionaries are the data structures available in Python that enable us to build long codes in a precise manner. Let’s have a quick recap of tuples. Tuples are a type of data structure in Python which are immutable and the elements of the tuple are enclosed within parentheses. For Example – tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) tup1 = (2.3, 4.6, 9.7) tup3 = (‘C’, ‘C++’, ‘JAVA’, ‘Python’, ‘R’) In this article, we will discuss the methods of converting a tuple to a string in Python-
Let us have a look at each one of them- Method – 1: Using for loopIn the first method, we will use for loop to convert Python tuple to string. In this program, for loop will help us to take each iterator from the tuple and apply a function to it. The following program shows how it can be done. Example – Output: PythonatJTP Let us understand what happened in the above program-
Method – 2: Using join() methodIn the next program, we shall see how the join() method help us to do the same- The join() method takes the items and combines them to form a string. The following program shows how it can be done- Example – Output: PythonatJTP Let us understand what happened in the above program-
Method – 3: Using reduce()Here we will make use of reduce for the conversion.The following program illustrates how it can be done in Python- Example – Output: PythonatJTP Explanation –
So, in this tutorial, we learned the various ways of converting a tuple to a string in Python. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263416.html