• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Strings / Convert Tuple to String in Python

Convert Tuple to String in Python

Author: Aditya Raj
Last Updated: May 5, 2022

In python, a tuple can contain different elements. On the other hand, a string is just a sequence of characters. In this article, we will discuss how to convert a tuple to a string when we are given a tuple of numbers of characters in python. 

Table of Contents
  • How to Convert Tuple of Characters to a String?
  • Convert Tuple of Numbers to a String
  • Conclusion

How to Convert Tuple of Characters to a String?

Suppose that we have the following tuple of characters.

myTuple = ("P", "y", "t", "h", "o", "n")

Now, we have to convert this tuple to the string “Python”.

To convert the tuple to string, we will first create an empty string named output_string. After that, We will traverse the tuple using a for loop. While traversal, we will add each character in the tuple to the output_string using the string concatenation operation. After execution of the for loop, we will get the desired string in the variable output_string. You can observe this in the following example.

myTuple = ("P", "y", "t", "h", "o", "n")
output_string = ""
for character in myTuple:
    output_string += character
print("The input tuple is:", myTuple)
print("The output string is:", output_string)

Output:

The input tuple is: ('P', 'y', 't', 'h', 'o', 'n')
The output string is: Python

Instead of using the for loop, we can use the join() method to convert the tuple of characters into a string. The join() method, when invoked on a string, takes an iterable object containing strings as an input argument. After execution, it returns the modified string containing the new character along with the characters in the original string. 

To convert a tuple of characters to a string, we will first create an empty string named  output_string. After that, we will invoke the join() method on the output_string with the tuple as input argument. After execution, the join() method will return the desired string as shown below.

myTuple = ("P", "y", "t", "h", "o", "n")
output_string = ""
output_string = output_string.join(myTuple)
print("The input tuple is:", myTuple)
print("The output string is:", output_string)

Output:

The input tuple is: ('P', 'y', 't', 'h', 'o', 'n')
The output string is: Python

Convert Tuple of Numbers to a String

If you try to convert a tuple of numbers to a string using any of the methods discussed above, the program will raise TypeError exception. You can observe this in the following example.

myTuple = (1, 2, 3, 4, 5, 6)
output_string = ""
for character in myTuple:
    output_string += character
print("The input tuple is:", myTuple)
print("The output string is:", output_string)

Output:

Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module>
    output_string += character
TypeError: can only concatenate str (not "int") to str

Similarly, when we use the second approach, the program runs into TypeError exception as shown below.

myTuple = (1, 2, 3, 4, 5, 6)
output_string = ""
output_string = output_string.join(myTuple)
print("The input tuple is:", myTuple)
print("The output string is:", output_string)

Output:

Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 3, in <module>
    output_string = output_string.join(myTuple)
TypeError: sequence item 0: expected str instance, int found

To avoid the error, we just have to convert each element of the tuple into a string before adding it to the output_string. In the first approach, we will first convert each element of the tuple into a string using the str() function. After that, we will perform the concatenation operation. In this way, we can convert a tuple of numbers to a string.

myTuple = (1, 2, 3, 4, 5, 6)
output_string = ""
for element in myTuple:
    character = str(element)
    output_string += character
print("The input tuple is:", myTuple)
print("The output string is:", output_string)

Output:

The input tuple is: (1, 2, 3, 4, 5, 6)
The output string is: 123456

For the approach using the join() method, we will first convert the tuple of numbers into a tuple of strings. For this, we will use the map() function and the str() function. 

The map() function takes a function as its first argument and an iterable object as the second input argument. After execution, it returns a map object in which the function is applied to each element of the iterable object. We will pass the str() function and the tuple of numbers to the map() function as input arguments. After that, we will convert the map object using the tuple() constructor. Hence, we will get a tuple of strings as shown below.

myTuple = (1, 2, 3, 4, 5, 6)
newTuple = tuple(map(str, myTuple))
print("The input tuple is:", myTuple)
print("The output tuple is:", newTuple)

Output:

The input tuple is: (1, 2, 3, 4, 5, 6)
The output tuple is: ('1', '2', '3', '4', '5', '6')

After getting the tuple of strings from the tuple of numbers, we can directly obtain the string as follows.

myTuple = (1, 2, 3, 4, 5, 6)
newTuple = tuple(map(str, myTuple))
output_string = ''.join(newTuple)
print("The input tuple is:", myTuple)
print("The output string is:", output_string)

Output:

The input tuple is: (1, 2, 3, 4, 5, 6)
The output string is: 123456

Conclusion

In this article, we have discussed how to convert a tuple to a string in python. To know more about strings in python, you can read this article on string formatting in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Strings Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us