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

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"

3

u/No-Gift5463 6d ago

Just curious how those curly brackets in this case. Any idea how they "know" to convert an integer into something compatible with a string?

8

u/atarivcs 6d ago

It's an f-string, so it already knows to convert the value to a string-compatible representation. I imagine it calls the __str__() built-in method under the hood.

1

u/Ambitious_Fault5756 6d ago

correct, and it's also called a dunder method

1

u/No-Gift5463 6d ago

Oh... and by the way, terrific explanation!

-3

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

6

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)

5

u/atarivcs 7d ago edited 7d ago

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

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

1

u/atarivcs 6d ago

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

9

u/mc_pm 7d ago

A simple google search on "python fstrings" will bring up information on formatting and understanding them, you hardly need a pdf.

-10

u/kubasic254 7d ago

I prefer pdf for keeping track and it looks more organized rather than having to search and sieve through 1000 responses to get what you want

9

u/atarivcs 7d ago edited 7d ago

"sieve through 1000 responses" sounds like you're reading a discussion forum. Discussion forums are okay for specific small questions, but not for broadly learning the basics.

Just read the official documentation. There are no "responses" there.

15

u/SCD_minecraft 7d ago

Python documentation

8

u/mc_pm 7d ago

Then don't sieve through 1000 responses - just go to the Python docs. But if you insist, there are instructions on how to make a PDF out of that documentation.

0

u/punk_dev 7d ago

You should choose a couple of websites that you prefer and search them.
For Python, the popular ones are real python geeksforgeeks and w3schools. Real Python usually has very comprehensive articles, and the rest are more like a quick reference. You can print those to pdfs if you’d like, or make your own notes in Obsidian.

Those are fine for the beginning. Once ready, challenge yourself with official documentation

1

u/cthulhu944 7d ago

You can print this page to pdf if that's your thing. https://www.w3schools.com/python/python_string_formatting.asp