Ways To Join The Elements Of A Tuple Into A String In Python

Solutions To Join The Elements Of A Tuple Into A String In Python

We will show you the most straightforward ways to join the elements of a Tuple into a string in Python.

Before we go to the methods section, let’s find out what Tuple is in Python.

What is a Tuple in Python?

Tuple is a collection of elements similar to List but immutable. Once an element is assigned to Tuple, it cannot be changed or reassigned. They are written almost the same way as List, but they have parentheses instead of brackets.

But if they are so similar to List and even more limited than List, why would we use Tuple instead of List? Because Tuple takes less memory space and, therefore, it is faster than List.

How to join the elements of a Tuple into a string in Python?

Method 1: Using the For Loop

For Loop syntax:

for value in sequence:
    #loop body

Parameters:

  • value: represents the element of the sequence on each iteration.
  • sequence: an iterable datatype such as a List, a Tuple, a Set, or a string.

We can use the For Loop to go through all the elements in the Tuple and join them into a string.

Like this:

tup = ('l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't')
myStr = ''

for item in tup:
  myStr = myStr + item

print(myStr)

Output:

learnshareit

Method 2: Using str.join()

Join() method syntax:

string.join(iterable)

Parameters:

  • iterable: an object that can be iterated upon, such as a List, a Tuple, a Set, or a string.
  • string: any string wrapped in quotes.

The string join() method returns a string by joining all elements of an iterable datatype (List, Tuple) and separated by the provided separator.

tup = ('l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't')

# join with whitespace
myStr = ' '.join(tup)

print(myStr)

Output:

l e a r n s h a r e i t

Method 3: Using join() and map() functions

Map() function syntax:

map(function, iterable)

Parameters:

  • function: the function that is called on each iteration to execute for each item.
  • iterable: an object that can be iterated upon, such as a List, a Tuple, a Set, or a string.

The str.join() function will fail when Tuple contains at least one non-string object(number, boolean). To avoid this TypeError, we will combine the map() function with the join() function. The str() function inside the map() function will take the element of the Tuple and convert it into a string.

#using join() and map()
def joinTuple(tup):
  result = ''.join(map(str, tup))
  return result

tup = ('l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't', 2022)
myStr = joinTuple(tup)

print(myStr)

Output:

learnshareit2022

Summary

So, in this post, we learned how to join the elements of a Tuple into a string in Python. You also better understand the For Loop, join() and map() function.

If you have any questions, leave a comment below.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *