String formatting for dictionary with f-string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

String formatting for dictionary with f-string in Python #

Alternate single and double quotes to perform string formatting for a dictionary with an f-string, e.g. result = f"id: {my_dict['id']}". The quotes the f-string is wrapped have to be different than the quotes inside the expressions, otherwise, the f-string is terminated prematurely.

main.py
my_dict = {'id': 0, 'name': 'Bob', 'salary': 100} # ✅ using square brackets to access dictionary keys result = f"id: {my_dict['id']}, name: {my_dict['name']}, salary: {my_dict['salary']}" print(result) # 👉️ id: 0, name: Bob, salary: 100 # ------------------------------------------- # ✅ using dict.get() to access dictionary keys result = f"id: {my_dict.get('id', 0)}, name: {my_dict.get('name', 'Anonymous')}, salary: {my_dict.get('salary', 1000)}" print(result) # 👉️ id: 0, name: Bob, salary: 100

The first example uses square brackets to access keys in the dictionary using a formatted string literal.

main.py
my_dict = {'id': 0, 'name': 'Bob', 'salary': 100} result = f"id: {my_dict['id']}, name: {my_dict['name']}, salary: {my_dict['salary']}" print(result) # 👉️ id: 0, name: Bob, salary: 100 result = f'Example dict: {my_dict}' print(result) # 👉️ Example dict: {'id': 0, 'name': 'Bob', 'salary': 100}

Notice that we wrapped the f-string in double quotes and used single quotes when accessing keys of the dictionary.

Had we used double quotes in the expressions in the f-string, we would have terminated the f-string prematurely.

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

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

Accessing a key that is not present in the dictionary raises a KeyError.

If you'd rather return a default value if the key is not present in the dictionary, use the dict.get() method.

main.py
my_dict = {'id': 0, 'name': 'Bob', 'salary': 100} result = f"id: {my_dict.get('id', 0)}, name: {my_dict.get('name', 'Anonymous')}, salary: {my_dict.get('salary', 1000)}" print(result) # 👉️ id: 0, name: Bob, salary: 100

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)
main.py
my_dict = {'id': 0} print(my_dict.get('id')) # 👉️ 0 print(my_dict.get('salary')) # 👉️ None print(my_dict.get('salary', 1000)) # 👉️ 1000

If a value for the default parameter is not provided, it defaults to None, so the dict.get() method never raises a KeyError.

The most important thing to note when using f-strings to perform string formatting with a dictionary is to alternate single and double quotes.

For example, if the f-string is wrapped in double quotes, use single quotes inside the expressions.

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.