Convert tuple to string in Python

In this Python tutorial, we will discuss the different methods to convert tuple to string in Python. We are all familiar that a Tuple stores several items in a single variable. It is also a fixed and well-organized collection of data. 

While Strings are groups of characters that are enclosed in single or double square brackets.

  • How to convert a tuple to string in Python by using the join() method
  • Convert the tuple to a string in Python by using the for loop
  • How to convert tuple to string in Python by using the reduce() method
  • How to convert tuple to string in Python with a comma
  • Conversion of a tuple to string in Python with spaces

Convert tuple to string in Python

The term “convert tuple into a string in Python” refers to the procedure of joining all the elements together to create a single string.

In Python, there are primarily three methods that are commonly used and important to understand when converting a tuple to a string. The following given methods are going to use for converting the tuple into a string.

  • By using the join() method
  • Reduce() method
  • for loop method

How to convert tuple to string in Python by using the join() method

  • Using the str.join() function, we can change a Python tuple into a Python string. The string join() method concatenates the strings in an iterable, such as a tuple, list, dictionary, or set, and returns a string.
  • Since the iterable in this instance is a tuple, the join() method, which returns the string, will accept the tuple as an argument.

Syntax:

Let’s have a look at the Syntax and understand the working of str.join() in Python.

str.join(iterable)

The join() method will produce a TypeError if any non-string values are present in the iterable.

Note: This parameter defines iterable objects like a tuple.

Example:

Let’s take an example and check How to convert the tuple to a string in Python by using the join() method.

Country_name = ('U.S.A', 'Australia', 'China', 'Belgium')
 
separator = ', '

new_str = separator.join(Country_name)
 
# Printing the results
print("Input tuple: ", Country_name)
print("Resultant Python string: ", new_str)
print(type(new_str))

Here is the implementation of the following given code

How to convert the tuple to string in Python by using the join method
By using the join method converted tuple to list

This is How to convert the tuple to a string in Python by using the join() method.

Read: Python program for finding greatest of 3 numbers

Convert the tuple to a string in Python by using the for loop

  • Here we will discuss How to convert the tuple to a string in Python by using the for loop
  • The whole tuple’s elements can be iterated on using a simple for loop, which will then append the elements to a string.
  • A tuple and an empty string are used. Moreover, the empty string is iterated through to include each element of the tuple.

Example:

Let’s take an example and check how we can convert the tuple to a string in Python by using the for loop.

Country_name = ("United States of America")
new_string = ''

for i in Country_name:
    new_string = new_string + i

print(new_string)

You can refer to the below Screenshot

How to convert the tuple to string in Python by using the for loop
Iterating values for tuples and converting them into a string

As you can see in the Screenshot we have discussed how to iterate values for tuples and convert them into a string.

Read: How to trim a string in Python

By using the reduce() method

  • A function and an iterable, such as a tuple, is the first and second arguments, respectively, of the reduce() function in Python.
  • The function is then applied to each element of the iterable object before returning the output of the operation that was taken out by the function.
  • High-order functions can interact with other functions due to the functools module.

Example:

Here we will take an example and check How to convert the tuple to a string in Python by using the reduce() method.

Source Code:

import functools
import operator
Cars_in_USA = ("Ford F-150, Chevrolet Silverado 1500")

# By using the reduce() function
new_result = functools.reduce(operator.add, (Cars_in_USA))
print ("Converted input tuple to string :",new_result)

Here is the Screenshot of the following given code.

How to convert the tuple to string in Python by using the reduce method
How to convert tuple to string in Python by using the reduce method

This is how we converted the tuple into a string by using the reduce method in Python.

Read: Python Extend Vs Append [Key Differences]

How to convert a tuple to string in Python with a comma

  • In this section, we will discuss how to convert the tuple to a string in Python with a comma delimiter.
  • To perform this particular task we are going to use the concept of the join method and the string join() method concatenates the strings in an iterable, such as a tuple, list, dictionary, or set, and returns a string.
  • Python’s join() function generates a new string by joining the other strings in the given iterable together which returns the string and will accept the tuple as an argument.

Example:

# Creation of input tuple
state_name_in_USA = ('Alaska', 'Georgia', 'Iowa','Missouri')

# Assign the ',' comma delimiter
comma_delim = ','
result= comma_delim.join(state_name_in_USA)

# Display the Content
print("Converted tuple into a string :", result)

In the following given code first, we created an input tuple and assign the elements within the enclosed brackets. The input string is named “state_name_in_USA”.

Now we want to use the comma as a separator when converting a tuple into a string. For this, we declared a variable and store the delimiter as a comma.

Here is the execution of the following given code.

How to convert the tuple to string in Python with a comma
How to convert tuple to string in Python with a comma

In this example, we have understood How to convert the tuple to a string in Python with a comma.

Read: Python program for binary search

Conversion of a tuple to string in Python with spaces

  • Here we will discuss how to convert the tuple to a string with spaces in Python.
  • By using join(), we can append the tuple’s characters and turn them into a string and in this example, we will get the space into the input string.
  • First, we will take an input tuple and assign the string elements to it. Now by Using whitespace as a delimiter and join() method, we can easily convert the tuple into a string.

Example:

Let’s take an example and check how to convert the tuple to a string with spaces in Python.

Country_name = ("United", "States", "of", "America")
# Using whitespace as a delimiter
new_string = "  "

# By using the join() method
result= new_string.join(Country_name)

# Display the Content
print(result)

Here is the Screenshot of the following given code.

How to convert the tuple to string in Python with spaces
Use white spaces as a delimiter to convert the tuple into a string

You may also like to read the following Python tutorial.

In this tutorial, we have discussed How to convert the tuple to a string in Python. And also we covered the following given topics

  • How to convert a tuple to string in Python by using the join() method
  • Convert the tuple to a string in Python by using the for loop
  • How to convert tuple to string in Python by using the reduce() method
  • How to convert tuple to string in Python with a comma
  • Conversion of a tuple to string in Python with spaces