Print a tuple with string formatting in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

Print a tuple with string formatting in Python #

Use a formatted string literal to print a tuple with string formatting, e.g. print(f'Example tuple: {my_tuple}'). Formatted string literals let us include expressions and variables inside of a string by prefixing the string with f.

main.py
my_tuple = ('a', 'b', 'c') # ✅ format a string with a tuple result = f'Example tuple: {my_tuple}' print(result) # 👉️ Example tuple: ('a', 'b', 'c') # ✅ access specific elements in the tuple result = f'first: {my_tuple[0]}, second: {my_tuple[1]}, third: {my_tuple[2]}' print(result) # 👉️ first: a, second: b, third: c # ---------------------------------------- # ✅ using the str.format() method result = 'first: {}, second: {}, third: {}'.format(*my_tuple) print(result) # 👉️ first: a, second: b, third: c # ---------------------------------------- # ✅ print a tuple without the parentheses result = ','.join(my_tuple) print(result) # 👉️ 'a,b,c'

The first example uses a formatted string literal to print a tuple with string formatting.

main.py
my_tuple = ('a', 'b', 'c') result = f'Example tuple: {my_tuple}' print(result) # 👉️ Example tuple: ('a', 'b', 'c')

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}.

You can use bracket notation to access a tuple element at index.

main.py
my_tuple = ('a', 'b', 'c') result = f'first: {my_tuple[0]}, second: {my_tuple[1]}, third: {my_tuple[2]}' print(result) # 👉️ first: a, second: b, third: c
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.

Alternatively, you can use the str.format() method.

main.py
my_tuple = ('a', 'b', 'c') result = 'Example tuple: {}'.format(my_tuple) print(result) # 👉️ Example tuple: ('a', 'b', 'c') result = 'first: {}, second: {}, third: {}'.format(*my_tuple) print(result) # 👉️ first: a, second: b, third: c

The str.format method performs string formatting operations.

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

You can use the iterable unpacking operator to unpack the tuple's elements in the call to the format() method if you need to access them in the string.

main.py
my_tuple = ('a', 'b', 'c') result = 'first: {}, second: {}, third: {}'.format(*my_tuple) print(result) # 👉️ first: a, second: b, third: c

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

The iterable unpacking operator unpacks the tuple and passes its elements as multiple, comma-separated arguments in the call to the str.format() method.

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

main.py
my_tuple = ('a', 'b', 'c') result = ','.join(my_tuple) print(result) # 👉️ 'a,b,c'

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.

We used a comma in the example, but you can use any other separator, e.g. an empty string to join the tuple's elements without a 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.