Search code examples
pythondictionaryformatf-string

How can I do a dictionary format with f-string in Python 3 .6?


How can I do this format with a 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']))

Solution

  • Well, a quote for the dictionary key is needed.

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