String formatting with Lists in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

String formatting with Lists in Python #

Use a formatted string literal to use string formatting with lists, e.g. f'The numbers are: {my_list[0]}, {my_list[1]}'. Formatted string literals enable us to include expressions inside of a string by prefixing the string with f.

main.py
my_list = [2, 4, 6] # ✅ string formatting with lists (using f-string) result = f'The numbers are: {my_list[0]}, {my_list[1]}, {my_list[2]}' print(result) # 👉️ The numbers are: 2, 4, 6 # ----------------------------------------------------- # ✅ string formatting with lists (using str.format()) result = 'The numbers are: {} {} {}'.format(*my_list) print(result) # 👉️ The numbers are: 2, 4, 6 # ----------------------------------------------------- # ✅ using string formatting with each item in a list result = [f'num: {item}' for item in my_list] print(result) # 👉️ ['num: 2', 'num: 4', 'num: 6']

The first example uses a formatted string literal to do string formatting with lists.

main.py
my_list = [2, 4, 6] result = f'The numbers are: {my_list[0]}, {my_list[1]}, {my_list[2]}' print(result) # 👉️ The numbers are: 2, 4, 6

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

main.py
my_list = [2, 4, 6] result = f'The list is: {my_list} and the first item is {my_list[0]}' print(result) # 👉️ The list is: [2, 4, 6] and the first item is 2

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

If you need to include items of the list in the string, access them at their specific indexes.

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

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

Use unpacking to use string formatting with lists, e.g. result = 'The numbers are: {} {} {}'.format(*my_list). The iterable unpacking operator will unpack the items of the list in the call to the str.format() method.

main.py
my_list = [2, 4, 6] result = 'The numbers are: {} {} {}'.format(*my_list) print(result) # 👉️ The numbers are: 2, 4, 6

The example uses the str.format() method instead of an f-string.

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 list has 3 items, so we used 3 replacement fields.

main.py
my_list = [2, 4, 6] result = 'The numbers are: {} {} {}'.format(*my_list) print(result) # 👉️ The numbers are: 2, 4, 6

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 list and passes its items as multiple, comma-separated arguments in the call to the str.format() method.

To perform string formatting for each item in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use a formatted-string literal to format each item in the list.
  3. The new list will contain the formatted strings.
main.py
my_list = [2, 4, 6] result = [f'num: {item}' for item in my_list] print(result) # 👉️ ['num: 2', 'num: 4', 'num: 6']

We used a list comprehension to iterate over the list.

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

On each iteration, we use a formatted string literal to format the current item and return the result.

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.