Python Quick Tip: F-Strings - How to Use Them and Advanced String Formatting

  Переглядів 199,689

Corey Schafer

Corey Schafer

День тому

In this Python Programming Tutorial, we will be learning how to use f-strings to format strings. F-strings are new to Python3.6+ and are extremely useful once you learn how to use them. Viewers have likely seen me use f-strings in previous videos so this video will go into detail exactly how to use them so that everyone can follow along confidently. Let's get started...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

КОМЕНТАРІ: 217
@sandeepvk
@sandeepvk 5 років тому
f string is god send. I am glad I started python in the post 3.6 era
@kamal3777
@kamal3777 3 роки тому
Scammers
@farajshaikh5100
@farajshaikh5100 2 роки тому
@@kamal3777 ?
@kamal3777
@kamal3777 2 роки тому
@@farajshaikh5100 i have no fucking idea
@stuthomas3593
@stuthomas3593 2 роки тому
Another excellent video from Corey Schafer. Easily the best python instructor on UKposts. I'll be switching to f-string from now on.
@lalligood
@lalligood 5 років тому
I've been using f-strings for a while but never about the colon formatting "tricks" before this. Nice!
@theegreatestever2420
@theegreatestever2420 2 роки тому
Its amazing how short but absolutely clear and excellently explained your videos are. Thank you from South Africa
@TheVerucAssault
@TheVerucAssault 2 роки тому
This is probably the best video I have seen explaining how F strings work. Thanks.
@ajkraftt5480
@ajkraftt5480 3 роки тому
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!
@doruletu1
@doruletu1 4 роки тому
Thanks Corey Schafer for f' string quick video explanation!
@WisomofHal
@WisomofHal 4 роки тому
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!
@dsuryas
@dsuryas 5 років тому
this is similar to template literals in javascript, awesome. Thanks, bro!
@eop771
@eop771 5 років тому
Thank you, Corey!
@ishpeace4886
@ishpeace4886 4 роки тому
well-explained and illustrated thanks Corey
@TechReptile
@TechReptile 3 роки тому
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.
@slavoie
@slavoie 5 років тому
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 :).
@dominikmoos4814
@dominikmoos4814 5 років тому
Sébastien Lavoie wouldn‘t it be easier to use str(i) ?
@slavoie
@slavoie 5 років тому
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'']...
@dominikmoos4814
@dominikmoos4814 5 років тому
I agree f-strings are awesome and thanks for the detailed answer!
@valentinkhomuteno6825
@valentinkhomuteno6825 5 років тому
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
@slavoie
@slavoie 5 років тому
Валентин Хомутенко thank you! That's very good to know!
@dylancooley9722
@dylancooley9722 3 роки тому
You have been a God send with this python playlist! Thank you so much, man.
@lucasflores1
@lucasflores1 3 роки тому
your english is clear, your teaching skills are amazing. Thanks!
@SACHIN-gd6zy
@SACHIN-gd6zy 5 років тому
Hey Buddy ur videos are of top quality
@KakaTu272
@KakaTu272 2 роки тому
Helpful video , easy and to the point . Thanks mate
@sampanicker4725
@sampanicker4725 4 роки тому
this video was very helpful. Thanks Corey!!
@parthibanspace
@parthibanspace 5 років тому
Keep doing this good work Corey! Thanks much.
@srikanth007spl
@srikanth007spl 5 років тому
Such a delightful feature! Thanks Corey.
@itsoktobewhite6377
@itsoktobewhite6377 Рік тому
You're killing it dude! 👍
@sladefx2955
@sladefx2955 2 роки тому
Learned a lot and the video is really well structured aswell. Thank you
@georgevarelas5073
@georgevarelas5073 5 років тому
The F-Strings are very useful. Thank you for this high quality video!!!
@andrealopez9556
@andrealopez9556 3 роки тому
Thank you so much! You explain it so much better than my teachers.
@jingyuchang1885
@jingyuchang1885 5 років тому
Very informative video! Thanks Corey!
@denniskamonde6836
@denniskamonde6836 5 років тому
Thanks Corey Best tutorials Ever
@monagulapa3022
@monagulapa3022 4 роки тому
Truly a Pro ! 👏👍
@timandrew4515
@timandrew4515 2 роки тому
much helpful! thank you!
@nonalcoho
@nonalcoho 3 роки тому
Thank you for your kind tutorial! I learn so much
@salkdjfasldkfjsdlk
@salkdjfasldkfjsdlk 5 років тому
Great tutorial as always.
@luizfernandes7104
@luizfernandes7104 Рік тому
Very helpful and straight forward!
@kelue3784
@kelue3784 5 років тому
I am in Shanghai, China. Watching your video is very helpful. I am very interesting about Python.
@jaysanprogramming6818
@jaysanprogramming6818 5 років тому
Good job! Again... Amazing!
@billstrum3203
@billstrum3203 3 роки тому
Good job Corey!!
@huntermaverick5114
@huntermaverick5114 5 років тому
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
@garydunken7934
@garydunken7934 5 років тому
Well planned tutorial with good examples. Thanks.
@JAYS040787
@JAYS040787 2 роки тому
Very helpful. Thank you
@RonaldModesitt
@RonaldModesitt 3 роки тому
Very helpful. Many thanks.
@kennethstephani692
@kennethstephani692 Рік тому
Great video, Corey!
@akira_asahi
@akira_asahi Рік тому
Thank you for the video. I am gratuful for your time and contribution. Kind regards, Akira.
@DreamwebMarocasa
@DreamwebMarocasa 4 роки тому
Thanks for sharing your knowlidge, Great stuff, very useful.
@AdamSmith-ux5if
@AdamSmith-ux5if 5 років тому
Love f-strings! Thx for another great video Corey :)
@WisomofHal
@WisomofHal 4 роки тому
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!
@jonathanwarner2420
@jonathanwarner2420 5 років тому
Well done, mate. Thx.
@sep69
@sep69 5 років тому
This sure is a handy feature. Thanks for the video :)
@mustinjiles8275
@mustinjiles8275 10 місяців тому
Thank you so much!
@novicetech1
@novicetech1 5 років тому
Awesome as always.
@x-Machina
@x-Machina 2 роки тому
This was great 👍🏻
@nazaserh
@nazaserh 3 роки тому
Great lesson 👍
@impossibleisnothing9197
@impossibleisnothing9197 5 років тому
Thank you for the video!
@arjunbarakoti4757
@arjunbarakoti4757 11 місяців тому
awesome , expecting more and more in python .
@SwinginBluesTube
@SwinginBluesTube 3 роки тому
your videos are really good. Thank you.
@tonglai7499
@tonglai7499 4 роки тому
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
@khitesh04
@khitesh04 5 років тому
Cool will solve some of my formating issues thanks bud
@MM-oq1lb
@MM-oq1lb 7 місяців тому
Can you upload a link to your previous video you mentioned in the beginning of this video?
@NathanY0ung
@NathanY0ung 5 років тому
already knew most of these except for the datetime one though, thanks :)
@GarimaJain
@GarimaJain 5 років тому
Thank you!
@saiganesh4361
@saiganesh4361 2 роки тому
Nice info corey
@guilhermehx7159
@guilhermehx7159 5 років тому
Thanks Corey
@akbarahmadi8175
@akbarahmadi8175 4 роки тому
great work
@kychemclass5850
@kychemclass5850 Рік тому
Thank you! :)
@prateeksarangi9187
@prateeksarangi9187 2 роки тому
Thanks again
@WindowzXp
@WindowzXp 4 роки тому
Saved me a headache. thank you.
@glutzm
@glutzm 5 років тому
Awesome content!
@gauravtiwari6392
@gauravtiwari6392 3 роки тому
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.
@claytonberger
@claytonberger 5 років тому
You are absolutely awesome!
@riddhiprajapati9948
@riddhiprajapati9948 5 років тому
Not only your video, You are amazing too.
@walber026
@walber026 5 років тому
Great one!
@noobinvestor3180
@noobinvestor3180 5 років тому
Precise,informative and exact..how do you do this Corey?
@eclypsed
@eclypsed 4 роки тому
AthulRaj Puthalath not even close... a lot of misinformation. .format() can use named placeholders and you can do functions when loading data in.
@rajveersingh2056
@rajveersingh2056 4 роки тому
@@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
@sharky2606
@sharky2606 4 роки тому
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
@amritdora9960
@amritdora9960 3 роки тому
good job!!!!
@user-mi8lq9zv7r
@user-mi8lq9zv7r 5 місяців тому
sweet swizzle bro. I am beginner python_man and run into some squat.BS and besides skipping through it I use power of UKposts and notice I gravitate towards your content more of ten than not. Time for burrito and beers brudda
@gingercholo
@gingercholo 4 роки тому
holy crap, you are awesome subbed!
@jongcheulkim7284
@jongcheulkim7284 2 роки тому
Thank you.
@randomguy75
@randomguy75 5 років тому
Thanks man. it would great if u made some videos about the new features in Python 3.6 and 3.7.
@jinqimao4781
@jinqimao4781 4 роки тому
Thanks!!!
@Hello-iq5ux
@Hello-iq5ux 3 роки тому
great video!
@maxvinella941
@maxvinella941 5 років тому
Excellent!
@dwaynerobare1153
@dwaynerobare1153 3 роки тому
I am a beginner... Really love your videos, the info and your approach to teaching. Please slow down your speaking a bit. Thanks again!
@iamparadox8885
@iamparadox8885 5 років тому
Sir you are amazing
@menghuajiang4314
@menghuajiang4314 2 роки тому
Easy to understand even as a newbie.
@thelurkingpanda3605
@thelurkingpanda3605 4 роки тому
You're a godsend
@chittipolinaidu1897
@chittipolinaidu1897 4 роки тому
excellent dear
@tamilstalin4609
@tamilstalin4609 5 років тому
Thankd bro
@davinderbhatia2573
@davinderbhatia2573 5 років тому
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
@maudentable
@maudentable 5 років тому
amazing stuff
@hanes2
@hanes2 5 років тому
Do you have tutorials on how to manage localisation in python? I only know the Qt way with using their own software
@susmitamazumder8390
@susmitamazumder8390 4 роки тому
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.
@francescowang
@francescowang 3 роки тому
Legend!!! Thank you. How do you comment and uncomment out using #?
@leenukes7418
@leenukes7418 5 років тому
Thanks dude ;-)
@abhisheksharma-ib5vw
@abhisheksharma-ib5vw 5 років тому
Nobody can beat you...only You, yourself can beat u... Incredible video
@thattoofunny
@thattoofunny 5 років тому
You Rock.....
@jianwenwu6948
@jianwenwu6948 5 років тому
Best video ever
@dwaynebinns3766
@dwaynebinns3766 2 роки тому
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.
@emmanuelchikeluba2360
@emmanuelchikeluba2360 4 місяці тому
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.
@shubhrajit2117
@shubhrajit2117 3 роки тому
At 3:55 how do you uncomment the lines without removing the # manually?
@francescowang
@francescowang 3 роки тому
This is what I'm trying to learn as well.
@vasudev16180
@vasudev16180 3 роки тому
Select multiple lines and then press ctrl + /. By the way, He has been using sublime text 3 editor.
@dengachol3024
@dengachol3024 3 роки тому
thanks
@Kabelman
@Kabelman 5 років тому
perfect
@notsotacticalninja2409
@notsotacticalninja2409 4 роки тому
Finally some nice explainatory video and step by step explained awesome, p.s. new sub :)
@YunikMaharjan
@YunikMaharjan 5 років тому
please please please do a video on asyncio. Thanks
@shivamathiya8384
@shivamathiya8384 5 років тому
Awesome Corey. Can u make a video on backend for "search bar in web app" using python2.7 and google data store .it would be a great help...
Python Tutorial: Generators - How to use them and the benefits you receive
11:14
F-strings In Python: Everything You Need To Know
23:29
ArjanCodes
Переглядів 48 тис.
Історія загиблого Назара Небожинського
00:54
Суспільне Рівне
Переглядів 1 млн
Піхотинець - про рутину на фронті
00:46
Суспільне Новини
Переглядів 1,2 млн
5 Useful F-String Tricks In Python
10:02
Indently
Переглядів 228 тис.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Переглядів 1 млн
How To Use Dunder Methods In Python Tutorial (Magic Methods)
6:36
Format specifiers in Python are awesome 💬
5:21
Bro Code
Переглядів 59 тис.
5 Useful Dunder Methods In Python
16:10
Indently
Переглядів 44 тис.
F-Strings Have A Lot of Format Modifiers You Don't Know
17:24
NeuralNine
Переглядів 11 тис.
Learn Python With This ONE Project!
55:04
Tech With Tim
Переглядів 1,6 млн
Modern Graphical User Interfaces in Python
11:12
NeuralNine
Переглядів 1,4 млн
f-strings in Python | Python Tutorial - Day #28
8:52
CodeWithHarry
Переглядів 267 тис.