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

Abou Menah picture Abou Menah · Apr 19, 2017 · Viewed 44.5k times · Source

How can i do this format with python 3.6 F-String ?

person = {'name': 'Jenne', 'age': 23}

print('My name {0[name]} and my age {1[age]}'.format(person, person))
print('My name {0} and my age {1}'.format(person['name'], person['age']))

Answer

M. Leung picture M. Leung · Apr 19, 2017

Well, a quote for the dictionary key is needed.

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