Print a tuple without parentheses in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Print a tuple without parentheses in Python #

Use the str.join() method to print a tuple without parentheses, e.g. result = ','.join(my_tuple). The str.join() method will return a string containing the tuple's elements without parentheses, with a comma separator.

main.py
# ✅ print tuple of strings without parentheses tuple_of_str = ('one', 'two', 'three') result = ','.join(tuple_of_str) print(result) # 👉️ 'one,two,three' # ----------------------------------------- # ✅ print tuple of integers without parentheses tuple_of_int = (1, 2, 3) result = ','.join(str(item) for item in tuple_of_int) print(result) # 👉️ '1,2,3' # ----------------------------------------- # ✅ print list of tuples without brackets and parentheses list_of_tuples = [(1, 2), (3, 4), (5, 6)] result = ','.join(','.join(str(item) for item in tup) for tup in list_of_tuples) print(result) # 👉️ '1,2,3,4,5,6'

We used the str.join() method to print a tuple without parentheses.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your tuple contains numbers or other types, convert all of the values to string before calling join().

main.py
tuple_of_int = (1, 2, 3) result = ','.join(str(item) for item in tuple_of_int) print(result) # 👉️ '1,2,3'

The example uses a generator expression to convert each integer in the tuple to a string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The string the join() method is called on is used as the separator between the elements.

main.py
my_tuple = ('one', 'two', 'three') my_str = ', '.join(my_tuple) print(my_str) # 👉️ "one, two, three"

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_tuple = ('one', 'two', 'three') my_str = ''.join(my_tuple) print(my_str) # 👉️ "onetwothree"

If you need to print the tuple's elements without parentheses and separated by spaces, call the str.join() method on a string containing a space.

main.py
my_tuple = ('one', 'two', 'three') my_str = ' '.join(my_tuple) print(my_str) # 👉️ "one two three"

If you need to print a list of tuples without brackets and parentheses, use 2 calls to the str.join() method.

main.py
list_of_tuples = [(1, 2), (3, 4), (5, 6)] result = ','.join(','.join(str(item) for item in tup) for tup in list_of_tuples) print(result) # 👉️ '1,2,3,4,5,6'

The inner call to the join() method joins the items of the tuple of the current iteration.

We used the str() class to convert each number to a string.

The last step is to use the join() method to join the tuples in the list into a string with a comma separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.