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.
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"]}'
.
Abou Menah
Updated on July 05, 2022Comments
-
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 about 7 yearserror 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 about 7 yearsJust 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 about 7 yearsThanks what about that? print('My name is {name} and my age is {age} and my salary is {salary}.'.format(**person))
-
M. Leung about 7 yearsThat 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 inf-string
, but auto converted from index value to the string instr.format()
-
jpoppe over 6 yearsThe 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 over 6 yearstrue 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'
vsf'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 almost 5 yearsYou could use the
format_map
function docs.python.org/3/library/stdtypes.html#str.format_map like thisprint('My name is {name} and my age is {age}.'.format_map(person))
-
MichaelChirico about 4 years@dbow is there any difference between
.format_map(dict)
and.format(**dict)
? -
ffi almost 3 yearsDid you enclose name and age in parentheses as proposed?
-
Spartacus about 2 yearsAlso note that this will not work in Python 3.5 or lower. F-strings were introduced in Python 3.6.