how to do a dictionary format with f-string in python 3.6?
Solution 1:
Well, a quote for the dictionary key is needed.
f'My name {person["name"]} and my age {person["age"]}'
Solution 2:
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
Solution 3:
Both of the below statement will work on Python 3.6 onward:
print(f'My name {person["name"]} and my age {person["age"]}')
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.