Print tuple elements in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Print tuple elements in Python #

Use the print() function to print a tuple in Python, e.g. print(my_tuple). If the value is not of type tuple, use the tuple() class to convert it to a tuple and print the result, e.g. tuple([1, 2]).

main.py
my_tuple = ('one', 'two', 'three') # ✅ print a tuple print(my_tuple) # 👉️ ('one', 'two', 'three') # ✅ print the first element in a tuple print(my_tuple[0]) # 👉️ 'one' # ✅ print the last element in a tuple print(my_tuple[-1]) # 👉️ 'three' # ✅ print a slice of a tuple print(my_tuple[0:2]) # 👉️ ('one', 'two') # ✅ print tuple without parentheses result = ','.join(my_tuple) print(result) # 👉️ 'one,two,three' # ✅ string formatting with tuples result = f'example tuple: {my_tuple}' print(result) # 👉️ example tuple: ('one', 'two', 'three')

We used the print() function to print a tuple's elements.

The print function takes one or more objects and prints them to sys.stdout.

If you have a tuple, you can pass it directly to the print() function to print it.

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

If you need to print an element in the tuple, access the element at its specific index.

main.py
my_tuple = ('one', 'two', 'three') # ✅ print the first element in a tuple print(my_tuple[0]) # 👉️ 'one' # ✅ print the last element in a tuple print(my_tuple[-1]) # 👉️ 'three' # ✅ print a slice of a tuple print(my_tuple[0:2]) # 👉️ ('one', 'two')

The syntax for tuple slicing is my_tuple[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first element in a tuple has an index of 0, and the last element has an index of -1 or len(my_tuple) - 1.

If you need to print a tuple without the parentheses, use the str.join() method.

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

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
my_tuple = ('one', 'two', 1, 2) all_strings = tuple(map(str, my_tuple)) print(all_strings) # 👉️ ('one', 'two', '1', '2') result = ''.join(all_strings) print(result) # 👉️ "onetwo12"

The string the 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 do string formatting with a tuple, use a formatted string literal.

main.py
my_tuple = ('one', 'two', 'three') result = f'example tuple: {my_tuple}' print(result) # 👉️ example tuple: ('one', 'two', 'three')

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ 'is subscribed: True'

Make sure to wrap expressions in curly braces - {expression}.

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.