Search code examples
pythondictionaryassociative-arrayf-string

How to use a variable as dictionary key using f-string in python?


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}"]}')

Solution

  • # Following part is working!!!
    print(f'Youve selected {languages[option]}')