One cool use of f-strings can be to iterate over a dictionary where the keys are strings of integers for some reason (using a JSON dictionary for instance) and get the values out of it directly with integers. No need to add an extra step to convert to strings, just use an f-string! Example: >>> mydict = {'0': 30, '1': 12, '2': 6, '3': 11, '4': 20} # using strings as keys >>> [mydict[f'{i}'] for i in range(4)] # list comprehension to retrieve values in dictionary based on integer >>> [30, 12, 6, 11] # list of values retrieved Hopefully someone will find that useful... or at least somewhat interesting :).
Dominik Moos it is in fact easier to read, but on my machine I can confirm that it is 30% faster to proceed with f-strings using the following test in a terminal: python -m timeit -s "mydict = {'0': 30, '1': 12, '2': 6, '3': 11, '4': 20}" "[mydict[str(i)] for i in range(4)]" → 200000 loops, best of 5: 1.7 usec per loop python -m timeit -s "mydict = {'0': 30, '1': 12, '2': 6, '3': 11, '4': 20}" "[mydict[f'{i}'] for i in range(4)]" → 200000 loops, best of 5: 1.18 usec per loop As to the reason why this is, I only know f-strings are evaluated at run time but I would be interested to learn more about this! For the time being, I will keep on using f-strings because they are awesome and they save at least two keystrokes every time ;) [str() vs f'']...
You don't want to go with this solution if you care about perfomance. list(mydict.values()) works well and also much faster than hand-written code. import time large_dict = {f'{i}':i*2 for i in range(10**6)} start = time.time() values = [large_dict[f'{i}'] for i in range(10**6)] end = time.time() print(f'done in {end-start}') start = time.time() values = list(large_dict.values()) end = time.time() print(f'done in {end-start}') >> done in 0.48581695556640625 >> done in 0.027498245239257812
This was the first time I liked, subscribed and hit the bell icon before being asked. This explains #fstrings on an intuitive level. Well done with the comparisons as well!
@@eclypsed you would have to use dot format function use every placeholder as a parameter. I think f string are way more compact than the format function
I was introduced to f-strings today in John Zelle's Intro to Computer Science: Python Programming book, which is very good and has taught me the Python I know, so far. I wasn't grasping the concept as quickly as I expected from the book, which has been the only thing I haven't been able to grasp within a few days so I came here to your channel. This makes f-strings look so sexy. Thanks a lot!
Wow! Came back to this video after letting f'strings mingle in my mind for a few weeks and this is even more golden haha. Really like the calculation exercise you showed uses f'string it's so cool Python allows us to run calculations within the {} Thanks again coach!
Corey Schafer in string format video: " String formatting allows us to display exactly the way we would like it" Me: Yes I love string formatting! Corey Schafer Fstring:"This is not elegant or intuitive" Me: bye string formatting
sweet swizzle bro. I am beginner python_man and run into some squat.BS and besides skipping through it I use power of UA-cam and notice I gravitate towards your content more of ten than not. Time for burrito and beers brudda
I understand what you mean by the syntax error that emanates from the wrong use of quotes, however, I used single quotes throughout in mine for the same example and it was perfectly printed on the terminal window without an error message.
Hi Corey. Great video as always. Can you make more videos about "Real World Example" like the one before? I really like this kind of video and I think they help us understand more of what we can actually do with Python. Thanks
Hey Corey, nice video you have there. I had a question though(I don't know whether it concerns with this video), I needed some explanation with the dunder method __format__(). How is it used? Can you provide some examples it would be great. Thanks for this video.
Hi Corey one of your video I have seen you comment out multiple lines at a single time. How did you do it from the keyboard? Sorry I could not remember which video it was though.
Corey 4really love your vids. I am new to.pyyhon and is a self thought person. I am having a hard time adding a line of values with the $ . I want to keep the $ as formatted.
f string is god send. I am glad I started python in the post 3.6 era
Scammers
@@kamal3777 ?
@@farajshaikh5100 i have no fucking idea
I've been using f-strings for a while but never about the colon formatting "tricks" before this. Nice!
Another excellent video from Corey Schafer. Easily the best python instructor on UA-cam. I'll be switching to f-string from now on.
Its amazing how short but absolutely clear and excellently explained your videos are. Thank you from South Africa
This is probably the best video I have seen explaining how F strings work. Thanks.
One cool use of f-strings can be to iterate over a dictionary where the keys are strings of integers for some reason (using a JSON dictionary for instance) and get the values out of it directly with integers. No need to add an extra step to convert to strings, just use an f-string!
Example:
>>> mydict = {'0': 30, '1': 12, '2': 6, '3': 11, '4': 20} # using strings as keys
>>> [mydict[f'{i}'] for i in range(4)] # list comprehension to retrieve values in dictionary based on integer
>>> [30, 12, 6, 11] # list of values retrieved
Hopefully someone will find that useful... or at least somewhat interesting :).
Sébastien Lavoie wouldn‘t it be easier to use str(i) ?
Dominik Moos it is in fact easier to read, but on my machine I can confirm that it is 30% faster to proceed with f-strings using the following test in a terminal:
python -m timeit -s "mydict = {'0': 30, '1': 12, '2': 6, '3': 11, '4': 20}" "[mydict[str(i)] for i in range(4)]"
→ 200000 loops, best of 5: 1.7 usec per loop
python -m timeit -s "mydict = {'0': 30, '1': 12, '2': 6, '3': 11, '4': 20}" "[mydict[f'{i}'] for i in range(4)]"
→ 200000 loops, best of 5: 1.18 usec per loop
As to the reason why this is, I only know f-strings are evaluated at run time but I would be interested to learn more about this! For the time being, I will keep on using f-strings because they are awesome and they save at least two keystrokes every time ;) [str() vs f'']...
I agree f-strings are awesome and thanks for the detailed answer!
You don't want to go with this solution if you care about perfomance.
list(mydict.values()) works well and also much faster than hand-written code.
import time
large_dict = {f'{i}':i*2 for i in range(10**6)}
start = time.time()
values = [large_dict[f'{i}'] for i in range(10**6)]
end = time.time()
print(f'done in {end-start}')
start = time.time()
values = list(large_dict.values())
end = time.time()
print(f'done in {end-start}')
>> done in 0.48581695556640625
>> done in 0.027498245239257812
Валентин Хомутенко thank you! That's very good to know!
Thanks Corey Schafer for f' string quick video explanation!
This was the first time I liked, subscribed and hit the bell icon before being asked. This explains #fstrings on an intuitive level. Well done with the comparisons as well!
I am in Shanghai, China. Watching your video is very helpful. I am very interesting about Python.
Hey Buddy ur videos are of top quality
your english is clear, your teaching skills are amazing. Thanks!
Precise,informative and exact..how do you do this Corey?
AthulRaj Puthalath not even close... a lot of misinformation. .format() can use named placeholders and you can do functions when loading data in.
@@eclypsed you would have to use dot format function use every placeholder as a parameter. I think f string are way more compact than the format function
he reads the documentation, and then makes these videos for people like me that are too lazy to read it and figure it out for themselves
I have followed your video and get courage to learn python.
Thanks god I have find you.
You are really a good guy. Thanks a lot.
Just carry on.
this is similar to template literals in javascript, awesome. Thanks, bro!
I was introduced to f-strings today in John Zelle's Intro to Computer Science: Python Programming book, which is very good and has taught me the Python I know, so far. I wasn't grasping the concept as quickly as I expected from the book, which has been the only thing I haven't been able to grasp within a few days so I came here to your channel. This makes f-strings look so sexy. Thanks a lot!
Wow! Came back to this video after letting f'strings mingle in my mind for a few weeks and this is even more golden haha. Really like the calculation exercise you showed uses f'string it's so cool Python allows us to run calculations within the {} Thanks again coach!
Such a delightful feature! Thanks Corey.
Thanks Corey Best tutorials Ever
You have been a God send with this python playlist! Thank you so much, man.
Thank you, Corey!
Thank you for the video. I am gratuful for your time and contribution. Kind regards, Akira.
Thank you so much! You explain it so much better than my teachers.
The F-Strings are very useful. Thank you for this high quality video!!!
Helpful video , easy and to the point . Thanks mate
this video was very helpful. Thanks Corey!!
Corey Schafer
in string format video: " String formatting allows us to display exactly the way we would like it"
Me: Yes I love string formatting!
Corey Schafer Fstring:"This is not elegant or intuitive"
Me: bye string formatting
Truly a Pro ! 👏👍
Nobody can beat you...only You, yourself can beat u... Incredible video
sweet swizzle bro. I am beginner python_man and run into some squat.BS and besides skipping through it I use power of UA-cam and notice I gravitate towards your content more of ten than not. Time for burrito and beers brudda
AMAZING! very clear video
Great tutorial as always.
I am a beginner... Really love your videos, the info and your approach to teaching. Please slow down your speaking a bit. Thanks again!
Well planned tutorial with good examples. Thanks.
Saved me a headache. thank you.
already knew most of these except for the datetime one though, thanks :)
very good and clear explanation
Keep doing this good work Corey! Thanks much.
Very informative video! Thanks Corey!
Cool will solve some of my formating issues thanks bud
awesome , expecting more and more in python .
well-explained and illustrated thanks Corey
Very helpful and straight forward!
Thanks for sharing your knowlidge, Great stuff, very useful.
Not only your video, You are amazing too.
Love f-strings! Thx for another great video Corey :)
I understand what you mean by the syntax error that emanates from the wrong use of quotes, however, I used single quotes throughout in mine for the same example and it was perfectly printed on the terminal window without an error message.
Very helpful. Many thanks.
Hi Corey. Great video as always. Can you make more videos about "Real World Example" like the one before? I really like this kind of video and I think they help us understand more of what we can actually do with Python. Thanks
This sure is a handy feature. Thanks for the video :)
Learned a lot and the video is really well structured aswell. Thank you
You're killing it dude! 👍
Good instructional video, thanks
At 3:55 how do you uncomment the lines without removing the # manually?
This is what I'm trying to learn as well.
Select multiple lines and then press ctrl + /. By the way, He has been using sublime text 3 editor.
Thank you for your kind tutorial! I learn so much
Great video, Corey!
Very helpful. Thank you
Easy to understand even as a newbie.
Well done, mate. Thx.
Good job! Again... Amazing!
Hey Corey, nice video you have there. I had a question though(I don't know whether it concerns with this video), I needed some explanation with the dunder method __format__(). How is it used? Can you provide some examples it would be great. Thanks for this video.
Legend!!! Thank you. How do you comment and uncomment out using #?
Good job Corey!!
much helpful! thank you!
great work
Just for the record, normal string formatting supports named arguments and key lookups.
sir your videos are greatest source! please can you make a video on url shortner in python . it would be really helpful. Thanks in advance
You're a godsend
Thanks Corey
Awesome as always.
Finally some nice explainatory video and step by step explained awesome, p.s. new sub :)
excellent dear
Thank you so much!
your videos are really good. Thank you.
good job!!!!
Thank you for the video!
Thanks man. it would great if u made some videos about the new features in Python 3.6 and 3.7.
This was great 👍🏻
Nice info corey
You are absolutely awesome!
Do you have tutorials on how to manage localisation in python?
I only know the Qt way with using their own software
Thank you!
Sir you are amazing
Great lesson 👍
Hi Corey one of your video I have seen you comment out multiple lines at a single time. How did you do it from the keyboard?
Sorry I could not remember which video it was though.
pretty good however would be better if you provide some extra examples for us to solve
Thank you.
Thank you! :)
amazing stuff
Corey 4really love your vids. I am new to.pyyhon and is a self thought person. I am having a hard time adding a line of values with the $ . I want to keep the $ as formatted.
Thanks again
Best video ever
Thanks!!!
Thanks dude ;-)
Excellent!
Can you upload a link to your previous video you mentioned in the beginning of this video?
great video!
Great one!
please please please do a video on asyncio. Thanks
You Rock.....
Awesome content!