String formatting with tuples in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

String formatting with tuples in Python #

Use a formatted string literal to perform string formatting with tuples, e.g. f'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 = (2, 4, 6, 8) # ✅ string formatting with a tuple result = f'Tuple: {my_tuple}' print(result) # 👉️ Tuple: (2, 4, 6, 8) # ✅ string formatting and accessing specific tuple elements result = f'first: {my_tuple[0]}, second: {my_tuple[1]}, third: {my_tuple[2]}, fourth: {my_tuple[3]}' print(result) # 👉️ first: 2, second: 4, third: 6, fourth: 8 # ---------------------------------------- # ✅ using the str.format() method result = 'first: {}, second: {}, third: {}, fourth {}'.format(*my_tuple) print(result) # 👉️ first: 2, second: 4, third: 6, fourth 8 # ---------------------------------------- # ✅ join a tuple's elements with a separator result = ','.join(str(item) for item in my_tuple) print(result) # 👉️ '2,4,6,8'

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

main.py
my_tuple = (2, 4, 6, 8) result = f'Tuple: {my_tuple}' print(result) # 👉️ Tuple: (2, 4, 6, 8)
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_tuple = (2, 4, 6, 8) result = f'Tuple: {my_tuple}, length: {len(my_tuple)}' print(result) # 👉️ Tuple: (2, 4, 6, 8), length: 4

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 = (2, 4, 6, 8) result = f'first: {my_tuple[0]}, second: {my_tuple[1]}, third: {my_tuple[2]}, fourth: {my_tuple[3]}' print(result) # 👉️ first: 2, second: 4, third: 6, fourth: 8
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 = (2, 4, 6, 8) result = 'first: {}, second: {}, third: {}, fourth {}'.format(*my_tuple) print(result) # 👉️ first: 2, second: 4, third: 6, fourth 8

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.

The tuple in the example has 4 elements, so we specified 4 replacement fields in the string.

main.py
my_tuple = (2, 4, 6, 8) result = 'first: {}, second: {}, third: {}, fourth {}'.format(*my_tuple) print(result) # 👉️ first: 2, second: 4, third: 6, fourth 8

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 join the tuple's elements with a separator into a string, use the str.join() method.

main.py
my_tuple = (2, 4, 6, 8) result = ','.join(str(item) for item in my_tuple) print(result) # 👉️ '2,4,6,8'

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

If your tuple stores strings, you can directly pass the tuple to the str.join() method.

main.py
my_tuple = ('one', 'two', 'three') result = ','.join(my_tuple) print(result) # 👉️ 'one,two,three'
Note that the str.join() method raises a TypeError if there are any non-string values in the iterable.

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.