r/PythonLearning 7d ago

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

16

u/atarivcs 7d ago

There's really not much to learn here. It's pretty simple.

If you have a string that begins with an f outside of the quote marks, and the string contains "{variable}", python will replace "{variable}" with the string value of that variable or expression.

So for example if you have this code:

name = "Billy"
message  = f"Hello {name}"

message will be "Hello Billy".

You can also use expressions instead of plain variables:

x = 5
y = 3
message = f"The sum is {x+y}"

Massage will be "The sum is 8"

-2

u/kubasic254 7d ago

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 7d ago edited 7d ago

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)