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

62,454

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:

  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.

Solution 4

Edit: confused the old format-function and the new f-stings. Added clarification.

The string pkuphy posted is correct, you have to use quotes to access the dictionary:

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

Your original string works for the str.format()-function:

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

My name is Jenne and my age is 23.

The first person references all occurences in the format-string, the second gives the variable to fill in.

Solution 5

This will work.

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

if name is a property of obj, f'name is {obj[name]}', but for a dict as in this question, you can direct access the key f'name is {person["name"]}'.

Share:
62,454
Abou Menah
Author by

Abou Menah

Updated on July 05, 2022

Comments

  • Abou Menah
    Abou Menah almost 2 years

    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']))
    
  • Abou Menah
    Abou Menah about 7 years
    error so that is why i am asking: f'My name {person[name]} and my age {person[age]}' NameError: name 'name' is not defined
  • M. Leung
    M. Leung about 7 years
    Just use the different quotes between a whole string and items. If you quote the string in double quotes, use single quotes for the keys. Both single quotes and double quotes act the same function in python.
  • Abou Menah
    Abou Menah about 7 years
    Thanks what about that? print('My name is {name} and my age is {age} and my salary is {salary}.'.format(**person))
  • M. Leung
    M. Leung about 7 years
    That is the str.format() method , the different between f-string and str.format is in how index lookups are performed, as you have to quote the key from dictionary in f-string, but auto converted from index value to the string in str.format()
  • jpoppe
    jpoppe over 6 years
    The question is about Python 3.6 f-strings explicitly, while you explain your answer clearly, this is not about what is being asked.
  • Mark Langford
    Mark Langford over 6 years
    true enough, I wrote this comment having recently put an f string in some of my own work like this: f'some text {mydict['mydumbmistake']} more text' vs f'some text {mydict["thisworks"]} more text' and visually the error was hard for me to spot (maybe I'm just rubbish), so love f strings as I do, its one place where the old style feels ligher on its feet and less chance of a dumbo like myself making yet another error.
  • dbow
    dbow almost 5 years
    You could use the format_map function docs.python.org/3/library/stdtypes.html#str.format_map like this print('My name is {name} and my age is {age}.'.format_map(person))
  • MichaelChirico
    MichaelChirico about 4 years
    @dbow is there any difference between .format_map(dict) and .format(**dict)?
  • ffi
    ffi almost 3 years
    Did you enclose name and age in parentheses as proposed?
  • Spartacus
    Spartacus about 2 years
    Also note that this will not work in Python 3.5 or lower. F-strings were introduced in Python 3.6.