how to do a dictionary format with f-string in python 3.6? how to do a dictionary format with f-string in python 3.6? python python

how to do a dictionary format with f-string in python 3.6?


Well, a quote for the dictionary key is needed.

f'My name {person["name"]} and my age {person["age"]}'


Depending on the number of contributions your dictionary makes to a given string you might consider using .format(**dict) instead to make it more readable, even though it doesn't have the terse elegance of an f string.

>>> person = {'name': 'Jenne', 'age': 23}>>> print('My name is {name} and my age is {age}.'.format(**person))My name is Jenne and my age is 23.

Whilst this option is situational, you might like avoiding a snarl up of quotes and double quotes


Both of the below statement will work on Python 3.6 onward:

  1. print(f'My name {person["name"]} and my age {person["age"]}')
  2. print(f"My name {person['name']} and my age {person['age']}")

Please mind the single ' and double " quotes in the above statements as placing them wrong will give syntax error.