In this tutorial, we will look at how to convert a tuple to a string in Python with the help of some examples.
How to convert a tuple to a string?
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.
Using string join()
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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()
.
Using a loop
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.