How to use a variable as dictionary key using f-string in python?
Question:
I’m trying to print dictionary item value of a dictionary but my item is also an another user input variable. How can I print the selected value with f-string?
option = ''
languages = {'1':'Learn Python', '2':'Learn Java', '3':'Learn C++', '4':'Learn PHP', '5':'Quit'}
option = input('Please choose an option between 1 and 5')
# Following part is not working!!!
print(f'Youve selected {languages["{option}"]}')
Answers:
# Following part is working!!!
print(f'Youve selected {languages[option]}')
You need use next string: print(f'Youve selected {languages[option]}')
f-string don’t not allow nested interpolation. However, to get the correct option from the dictionary you don’t need quotes, languages[option]
will do.
So the line you are looking to use will be
print(f"You've selected {languages[option]}")
option is a variable, but you made option to a string. That means the key you gave the dictionary language is in your case {option} and this key doesnt exist
languages = {'1':'Learn Python', '2':'Learn Java', '3':'Learn C++', '4':'Learn PHP', '5':'Quit'}
option = str(input('Please choose an option between 1 and 5'))
# Following part is working!!!
print(f'Youve selected {languages[option]}')
You didnt use the variable option, you used {option} as a string and tried to print the value of the key {option}. Just remove the brackets and the "".
You could also use a f string in a f string, but thats not necessary.
print(f'Youve selected {languages[f"{option}"]}')
The reason, why your code didnt work is in that case, because you made a string in a f string and not a f string in a f string
I’m trying to print dictionary item value of a dictionary but my item is also an another user input variable. How can I print the selected value with f-string?
option = ''
languages = {'1':'Learn Python', '2':'Learn Java', '3':'Learn C++', '4':'Learn PHP', '5':'Quit'}
option = input('Please choose an option between 1 and 5')
# Following part is not working!!!
print(f'Youve selected {languages["{option}"]}')
# Following part is working!!!
print(f'Youve selected {languages[option]}')
You need use next string: print(f'Youve selected {languages[option]}')
f-string don’t not allow nested interpolation. However, to get the correct option from the dictionary you don’t need quotes, languages[option]
will do.
So the line you are looking to use will be
print(f"You've selected {languages[option]}")
option is a variable, but you made option to a string. That means the key you gave the dictionary language is in your case {option} and this key doesnt exist
languages = {'1':'Learn Python', '2':'Learn Java', '3':'Learn C++', '4':'Learn PHP', '5':'Quit'}
option = str(input('Please choose an option between 1 and 5'))
# Following part is working!!!
print(f'Youve selected {languages[option]}')
You didnt use the variable option, you used {option} as a string and tried to print the value of the key {option}. Just remove the brackets and the "".
You could also use a f string in a f string, but thats not necessary.
print(f'Youve selected {languages[f"{option}"]}')
The reason, why your code didnt work is in that case, because you made a string in a f string and not a f string in a f string