Javaexercise.com

How to Convert Tuple to String in Python

In this topic, we will learn to convert Python tuple to string. The tuple is an immutable data structure that is used to store elements while the string is a sequence of characters enclosed within single or double-quotes.

Here, we have several examples to understand the conversion between tuple to string and vice versa.

To convert a tuple to a string, we used join() method that is a string class method and used to get a string from an iterable. In the second example, we are using map() method with join() to cast non-string elements in the tuple. If we do not use the map() method then we get an error at runtime due to string conversion.

Let's see examples to understand the conversion of a tuple to string and vice versa.

Example: Python Tuple to String Conversion using join() Method

Here, we are converting tuple to string type using the join() method. This method returns a string from the iterable: list, tuple, etc. Here, the type() method is used to check the type of value after conversion to ensure that conversion is successful.

# Take a tuple
tuple_val = ("python","is","easy","to","learn")
print(tuple_val)
# Convert tuple to string
str_val = " ".join(tuple_val)
# Print value and it's type
print(str_val)
print(type(str_val))

Output:

('python', 'is', 'easy', 'to', 'learn')
python is easy to learn
<class 'str'>

Tuple to String conversion using join() and map() Methods

If we have a tuple that contains non-string elements such as integer or float then we must use map() method, otherwise the join() method raises a TypeError. Here, we are using the map() method inside the join() method to avoid any type of error.

# Take a tuple
tuple_val = ("python",3.8,"is","easy","to","learn")
print(tuple_val)
# Convert tuple to string
str_val = " ".join(map(str,tuple_val))
# Print value and it's type
print(str_val)
print(type(str_val))

Output:

('python', 3.8, 'is', 'easy', 'to', 'learn')
python 3.8 is easy to learn
<class 'str'>

Converting String to Tuple in Python

After learning the conversion of a tuple to string. Now, let's learn the conversion of string to tuple which is exactly the reverse process of the previous one. Conversion of string to a tuple is very easy, we just need to add a comma after the string variable and enclose them within a parenthesis.

Note: You must add a comma after the variable else result would be a string rather than a tuple.

# Take a string
str_val = "python is easy to learn"
print(str_val)
# Convert string to tuple
tuple_val = (str_val,)
# Print value and it's type
print(tuple_val)
print(type(tuple_val))

Output:

python is easy to learn
('python is easy to learn',)
<class 'tuple'>


Conclusion

Well, in this topic, we learnt to convert tuple to string and vice versa. We used join() and map() functions to convert tuple to string and a single comma within parenthesis to convert string to tuple type.

If we missed something, you can suggest us at - info.javaexercise@gmail.com