Convert tuple to a string in python

Convert Tuple to a String in Python

In this tutorial, we will look at how to convert a tuple to a string in Python with the help of some examples.

Convert tuple to a string in python

You can use the Python string join() function to convert a tuple to a string. Alternatively, you can also iterate through the elements in the tuple and add them to your result string.

Let’s look at both the methods with the help of some examples.

The string join() function is used to join strings together with a custom string in between them. To join concatenate strings, you can use an empty string, "" to join two strings or strings in an iterable.

Let’s look at an example.

# tuple of strings
t = ("a", "p", "p", "l", "e")
# get string from tuple
print("".join(t))

Output:

apple

We get the string resulting from joining the characters in the tuple.

In the above example, we used a tuple that contained only string values. What would happen if our tuple contains non-string values? Let’s find out.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# tuple of strings
t = ("a", "p", "p", "l", "e", 2)
# get string from tuple
print("".join(t))

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <module>
      2 t = ("a", "p", "p", "l", "e", 2)
      3 # get string from tuple
----> 4 print("".join(t))

TypeError: sequence item 5: expected str instance, int found

We get an error. This is because the string join() function only works on strings (the element 2 is not a string). If you’re not sure above the type of element in the tuple, you can convert them to string before you apply the join() function.

# tuple of strings
t = ("a", "p", "p", "l", "e", 2)
# get string from tuple
print("".join(str(i) for i in t))

Output:

apple2

We now get the results without any errors. Here, we use the str() function to convert each element in the tuple to a string before applying the string join().

You can also iterate through the elements in a tuple and add them to a result string. Let’s look at an example.

# tuple of strings
t = ("a", "p", "p", "l", "e")
# get string from tuple
result = ""
for c in t:
    result = result + c
print(result)

Output:

apple

We get the string resulting from the tuple elements.

Again, if the tuple has non-string elements, convert them to string before concatenating them.

# tuple of strings
t = ("a", "p", "p", "l", "e", 2)
# get string from tuple
result = ""
for c in t:
    result = result + str(c)
print(result)

Output:

apple2

We get the correct result.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top