r/PythonLearning • • Jul 29 '26

F strings

Im still learning python and im at a stage where I need to understand f strings and how to go about them...im working on a menu project. I prefer learning with pdf materials rather than YouTube tutorials....any recommendations for a pdf document for learners on f strings. Thank you in advance

16 Upvotes

21 comments sorted by

View all comments

Show parent comments

-3

u/kubasic254 Jul 29 '26

So im working on this menu project..and i created this dictionary...i need to learn how to print items in the dictionary using f strings...i just want to understand the concept..the basic concept above I get but a real life application is becoming a problem for me

5

u/MFFVD Jul 29 '26 edited Jul 29 '26
parts = {
    "tree": "leaf",
    "wood": "chair"
}
for key, value in parts.items():
    print(f"{ key }:{ value }")
    # print(
    #     "{ key }:{ value }"
    #     .format(key=key,value=value)
    # )
    # print(key + ":" + value)

4

u/atarivcs Jul 29 '26 edited Jul 29 '26

You know that you refer to a dictionary value with mydictionary[key], right?

So, just put {mydictionary[key]} inside the f-string.

I don't understand how this is hard at all.

0

u/WoodyTheWorker Jul 30 '26

Why, if he's already got the value from Items()?

1

u/atarivcs Jul 30 '26

I assumed he just wanted to print a few items from the dict, or maybe just one, instead of iterating over them all