5 Useful F-String Tricks In Python

  Переглядів 227,943

Indently

Indently

День тому

Here are my top 5 most useful f-string formatting tricks that I use everyday in Python.
▶ Valentine's Day SALE on indently.io:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels

КОМЕНТАРІ: 302
@chyldstudios
@chyldstudios 2 місяці тому
the "=" trick in the f-string is fire.
@m-zurowski
@m-zurowski 2 місяці тому
It made my day - I'm gonna test run it in a loop with some random dictionary to see how many cool tricks it can do (like reading current settings) :)
@m-zurowski
@m-zurowski 2 місяці тому
ok, my idea didn't work as expected 😅
@dark_brownie
@dark_brownie 11 днів тому
Yeah, completely agree it is amazing
@sunwukong6268
@sunwukong6268 3 місяці тому
Ever since I learned f-strings...I love them.
@GentleMannOfHats
@GentleMannOfHats 2 місяці тому
I wish I had known sooner!!
@rickymort135
@rickymort135 2 місяці тому
And I love you.... Sorry if I made things awkward... 😬 Oof this awkward isn't it?
@_Loki__Odinson_
@_Loki__Odinson_ 3 місяці тому
This is the first time I have seen someone specify datatype for variables in python, and I honestly loved it. Great tips btw.
@Indently
@Indently 3 місяці тому
That's the hype for type annotations that I love to see!
@Naej7
@Naej7 2 місяці тому
Not using type annotations should be banned by law
@_Loki__Odinson_
@_Loki__Odinson_ 2 місяці тому
@Naej7 I don't think so, many people choose Python for its simplicity, with the absence of type annotations being one of the key factors. Removing this feature might deter beginners from trying it out. However, as you become more proficient in programming, you may choose to utilize type annotations or when exploring other languages.
@Naej7
@Naej7 2 місяці тому
@@_Loki__Odinson_ Type Annotations help a lot, and if adding 5 characters (: int) is too hard at the beginning, then one should give up on programming lmao
@edwardcullen1739
@edwardcullen1739 2 місяці тому
​@@Naej7 Except people don't understand how to use them, so you end up with unnecessarily hard to use APIs. If that's how you feel, why not just use C? 🤦‍♂️
@andymitchell2146
@andymitchell2146 2 місяці тому
I've been using python for about 10 years, and f strings extensively, but never knew that last tip! Game changer!
@yash1152
@yash1152 2 місяці тому
well, thats cause its only a very recent one (afaik)
@eyehear10
@eyehear10 Місяць тому
@@yash1152it was introduced in 3.8, so a while back
@Zenivo
@Zenivo 2 місяці тому
About the fourth trick: the f in ".2f" tells it to format as float. You can also do for example ".2e"" which will format the number in scientific format.
@utarasama
@utarasama 3 місяці тому
The last one is super duper cool!
@dcx45
@dcx45 2 місяці тому
RIP the maintainer
@utarasama
@utarasama 2 місяці тому
​@@dcx45 he should then watch this video
@krzysiekkrzysiek9059
@krzysiekkrzysiek9059 3 місяці тому
This kind of tips are awesone. We need more 👍
@TheMcSebi
@TheMcSebi 3 місяці тому
Great video! Didn't know about the datetime and debug print ones. Definitely going to use them in the future, though.
@bashar9200
@bashar9200 2 місяці тому
This is amazing!! thank you for this tutorial!!
@jaa928
@jaa928 2 місяці тому
Thank you for the instructive tips!
@SergioYT2052
@SergioYT2052 2 місяці тому
"Simple y bello como un anillo", como diría Neruda; pero además, muy funcional. ¡Muchas gracias!
@flashtrack101
@flashtrack101 2 місяці тому
Love your vids man! would love to see a tutorial on cython from you!
@dipeshsamrawat7957
@dipeshsamrawat7957 3 місяці тому
Nice collection! 💯
@rolandsz8831
@rolandsz8831 2 місяці тому
Great video! I missed the bonus tip where you explain that format string calls __format__ on the object being formatted, so you can do your own formatting, like this: class MyData: def __init__(self, a: int, b: int, c: int): self.a = a self.b = b self.c = c def __format__(self, spec): if spec[0] not in self.__dict__: sep = spec[0] l = list(spec[1:]) else: sep = ',' l = list(spec) return sep.join(str(self.__dict__[key]) for key in l) my_var = MyData(a=1, b=2, c=3) assert f"{my_var:cba}" == "3,2,1" assert f"{my_var:-abc}" == "1-2-3"
@TheJaguar1983
@TheJaguar1983 3 місяці тому
Didn't know about the date/time and equals formatting. Looks like the first one forwards to strftime. Makes things so much more concise and readable.
@richsadowsky8580
@richsadowsky8580 2 місяці тому
Absolutely fantastic and useful video!
@timegor844
@timegor844 3 місяці тому
Wow, so many simple things I didn't about... Thank you
@mjhaynavarro
@mjhaynavarro Місяць тому
So cool.. thanks for sharing it. very informative
@MahdiImeni
@MahdiImeni 2 місяці тому
Loved it ❤
@kychemclass5850
@kychemclass5850 2 місяці тому
Love #5. Thank you.
@viniciomonge3960
@viniciomonge3960 Місяць тому
Loved the last trick!!!
@TheKahunas2722000
@TheKahunas2722000 2 місяці тому
Love the video I knew some of those but the last one is epic I will be using that from now on .
@BohumirZamecnik
@BohumirZamecnik 22 дні тому
Very nice. Another useful is formatting float as percent: f"{foo:.2%}".
@rahulCoding
@rahulCoding 2 місяці тому
Great video.... Thanks a lot😍👍
@Angelinajolieshorts
@Angelinajolieshorts 2 місяці тому
Great work sir❤
@mattshu
@mattshu 3 місяці тому
F strings are soo chef kiss
@cmcdougle
@cmcdougle 2 місяці тому
Thank you, this went STRAIGHT into my current project. Commas in numbers was one of the next things I was going to look up.
@ChrisHalden007
@ChrisHalden007 2 місяці тому
Great video. Thanks
@griffgruff1
@griffgruff1 2 місяці тому
Great video!
@Pawlo370
@Pawlo370 3 місяці тому
Finaly! All fstring variations in one film
@LittleGnawer
@LittleGnawer 2 місяці тому
Nice and useful tricks for every day programming. I also prefer specifying types of variables, since it makes code better understandable.
@kinngrimm
@kinngrimm 2 місяці тому
thanks for the showcases
@quekki3666
@quekki3666 3 місяці тому
i love f strings also this is like the 3rd time i come across the = specifier but i keep forgetting its existence and type in the whole thing
@Indently
@Indently 3 місяці тому
It's a cool trick for sure!
@catastrophicblues13
@catastrophicblues13 2 місяці тому
That last one is sooo useful!
@dark-ghost4132
@dark-ghost4132 3 місяці тому
Thank for nice tricks 😘
@sidjay7644
@sidjay7644 2 місяці тому
Very nice Thanks
@SaveCount-bh8tp
@SaveCount-bh8tp 3 місяці тому
Thanks very much
@karthikkarthik100
@karthikkarthik100 3 місяці тому
Last trick was super cool...
@Anzeljaeg
@Anzeljaeg 2 місяці тому
This is pure gold 🥇
@Al_Miqdad_
@Al_Miqdad_ 3 місяці тому
thanks for your time please make videos about data structure
@mrjamesflores
@mrjamesflores 3 місяці тому
The last one was good!
@Krullfath
@Krullfath 3 місяці тому
This is super cool, I sadly can't think of any usecases in my current project
@Andrey_Fedorov
@Andrey_Fedorov 2 місяці тому
Круто! Спасибо!
@Sailesh_Bhoite
@Sailesh_Bhoite 2 місяці тому
Nice Tricks!
@kinngrimm
@kinngrimm 2 місяці тому
Does datetime have a formating depending on nation? Like we would have already through a login or a whois access to a users current or defined whereabouts or nationality and we would want to have their specific way of reading time provided for them.
@UndyingEDM
@UndyingEDM Місяць тому
For those who didn't know, the last one is called self-documenting expression and was released in Python 3.8
@artistpw
@artistpw Місяць тому
Very nice.
@cheesy_boya
@cheesy_boya 3 місяці тому
Yep, I'm saving this video
@tolgaflashtr2855
@tolgaflashtr2855 3 місяці тому
too* 😂😂
@aguy9836
@aguy9836 2 місяці тому
nice examples
@adeptusmortem
@adeptusmortem 2 місяці тому
Thank you
@aliwalil4160
@aliwalil4160 2 місяці тому
the last fstring was dope
@daveys
@daveys 2 місяці тому
I like that print(f’{a + b = }’) one at the end. I can think of a few times when I’d use that.
@Indently
@Indently 2 місяці тому
Earlier I didn't know it worked on whole expressions, I think it's super cool as well!
@oldschoolsoldier1634
@oldschoolsoldier1634 3 місяці тому
Trick #2 is neat
@GW-nh9qc
@GW-nh9qc Місяць тому
Klasse 😃👍
@yash1152
@yash1152 2 місяці тому
i knew last one already, but seeing it again made me realise i should try to do this in java too, would have shortened a whole lot of cruft in one program i made.
@noir66146
@noir66146 2 місяці тому
ooh i like these videos logic magic !
@fg786
@fg786 3 місяці тому
What can you do with print(f'{var: >+{x}}') ? The additional + get's printed in front of var but a - doesn't, you can put a # instead of the + and it's not throwing an error, yet doesn't seem to do anything. Letters and other symbols give an error.
@xKiiyoshiix
@xKiiyoshiix 3 місяці тому
Hello @Indently, Can you please explain me, why you use ":" after a variable for ex. n:? Regards.
@matthewbay1978
@matthewbay1978 3 місяці тому
It allows him to specify what type of variable it is. "n: int = " tells anyone reading that it's an integer. I'm glad you asked that though, because I'm an amateur and I'm curious, @Indently is that common practice?
@Little-bird-told-me
@Little-bird-told-me 7 днів тому
Good Job
@swolekhine
@swolekhine 3 місяці тому
These will be useful to me for sure. Here's a comment for the algorithm gods!
@Mor3Lif3
@Mor3Lif3 Місяць тому
That last one goes wild
@kinngrimm
@kinngrimm 2 місяці тому
9:30 that approach though had the single quotation marks removed, which from a formating pov is cleaner, isn't there a version of the first shorter approach without the quotation marks then printed? (edit: while still being flexible in terms of variable names as mentioned)
@ErLakhan
@ErLakhan 28 днів тому
Superb
@MechanicusOfficial
@MechanicusOfficial Місяць тому
I always use pyformat. Very easy to understand and pretty nice too. Var = 15 print(“this is my var: {}”.format(var))
@gopal.
@gopal. Місяць тому
thanks
@chrisogonas
@chrisogonas 2 місяці тому
Awesome! I love the '=' and >
@tudaer
@tudaer 2 місяці тому
May I ask which IDE and development env are you using? Looks so great
@meowsqueak
@meowsqueak 2 місяці тому
It’s PyCharm
@tudaer
@tudaer 2 місяці тому
@@meowsqueak thanks!
@NC700xLover
@NC700xLover 2 місяці тому
Which editor is that? Looks fresh
@-george-1153
@-george-1153 Місяць тому
You're cool! 👍
@semenivanoff8615
@semenivanoff8615 Місяць тому
Useful. Thanks
@sayantanguha1934
@sayantanguha1934 2 місяці тому
The last one blew my mind
@mikaelregetz6224
@mikaelregetz6224 2 місяці тому
what theme do you use pycharm
@MkReman
@MkReman 3 місяці тому
Which code editor do you use? It appears to be pycharm. But the ui is different from what I have in windows laptop.
@meowsqueak
@meowsqueak 2 місяці тому
It’s the new UI. Perhaps you’re still using the legacy UI?
@user-hd3pz2ow1b
@user-hd3pz2ow1b 2 місяці тому
rounding in python .. cool
@richardnicholas2957
@richardnicholas2957 2 місяці тому
Gold!
@meghanelizondo774
@meghanelizondo774 2 місяці тому
I was so stoked when f'{ var = }' was added to Python!! Might be abusing it a bit lol
@andrewmalani1882
@andrewmalani1882 Місяць тому
what complier/interpreter does he use?
@ArtyomKatsap
@ArtyomKatsap 2 місяці тому
Hi! Thanks! Great video! Worth mentioning that the last one does not work on older versions of python3 (I tried it on 3.7.17 and it gives a syntax error).
@UndyingEDM
@UndyingEDM Місяць тому
Probably added in a later version. I'd love to know which. Edit: it's called self-documenting expression released in Python 3.8
@acherongoon
@acherongoon 3 місяці тому
My preference is th .format(...) method fo a couple o reasons. I use Micropython a lot and f""" is not or has not been available, for format supports all the styles I like. i8n the string being formatted is not known at development time. Scope, the names using inside the string can be assigned at use time, i.e. in a function the value may be in a variable gmt_time but the string uses a standard name 'time'.
@mad_vegan
@mad_vegan 3 місяці тому
To use scientific notation with integers, you can either do int(2e9) or 2*10**9.
@veeratheking8375
@veeratheking8375 3 місяці тому
big fan bro from india
@Indently
@Indently 3 місяці тому
Thanks!
@shabadooshabadoo4918
@shabadooshabadoo4918 3 місяці тому
i dont really know anything about programming or python but im curious. when you do "var:" is the colon part of the variable name? or a requirement for specifying a variable name? if its part of its name, is it to differentiate it from something else?
@AnonHooman
@AnonHooman 2 місяці тому
The colon after a variable name is for typing the variable, for example: my_var: str = ‘Hello’ We’re saying that my_var is of type str (string) and has the value ‘Hello’. However, the typing is not necessary and my_var = ‘Hello’ would work just as well. Though, most people would recommend to use type hints
@PanduPoluan
@PanduPoluan 2 місяці тому
@@AnonHooman Well if assigning simple, easy-to-infer data for a variable, I usually do away with type-hinting. Type-hinting is a godsend to mark parameters to a function, preventing hard to trace ValueError or TypeError exceptions later on. I usually don't type hints my variables except when I'm doing unpacking.
@NickCombs
@NickCombs 2 місяці тому
This definitely seems more convenient than it is in js.
@casperghst42
@casperghst42 28 днів тому
Intersting, a large part of the world is using . (period) as a 1000 separator and , (comma) as a decimal point.
@brycesakal3717
@brycesakal3717 3 дні тому
My professor who I took intro to Python called the “” ‘right justify and left justify’
@0MVR_0
@0MVR_0 2 місяці тому
the thumbnail to this video inspired an idea to use format strings in dictionaries so that a dynamic series of texts can undergo a linear list of mutations. Honestly, I actually need to refine my idea though.
@roshanpandey1498
@roshanpandey1498 2 місяці тому
What editor is this?
@iscatafan2959
@iscatafan2959 3 місяці тому
You make the code easier to read, so you better use the functions of the str class! 🐳
@philluvschips3787
@philluvschips3787 2 місяці тому
Hi, if I have a number 1234.5678, how do I use a combination of , separators make it 2 decimal places so I get 1,234.56?
@Smart_Coder7
@Smart_Coder7 3 місяці тому
This Video was sponsored by me. 💀
@bilatungdulang9708
@bilatungdulang9708 2 місяці тому
Ehem yo bro, are the "int" word after name variable is static type like variable declaring in rust "let num : i32 = 1000000000"?
@nadkoch
@nadkoch Місяць тому
No sense, because type sets through assignment. any = 15 If was: any: const = 15 Еlse we have repeat type assignment. any : int = int (15). 🤷‍♂️
@midlander8186
@midlander8186 2 місяці тому
In the first example, int variable n assigned a number in scientific notation returns a float number when printed, but no error occurred. In what sense must n be declared a float therefore?
@meowsqueak
@meowsqueak 2 місяці тому
It’s not a type declaration, it’s a type hint given as an annotation. Annotations in Python have no effect at run time so no error occurs. They are just hints to a static type checker.
@isodoubIet
@isodoubIet 2 місяці тому
Type annotations are just comments, and comments always lie.
@kinngrimm
@kinngrimm 2 місяці тому
in the first example, do you know what decimal points wouldn't work?
@archiemarqx
@archiemarqx 3 місяці тому
3:24 this tip will literally improve my code quality
@annieshedden1245
@annieshedden1245 2 дні тому
i'm old but i still think everyone should know that most of the f-string stuff comes from C printf/strftime/etc.
@pmenown
@pmenown 2 місяці тому
What does the f prefix do at the beginning of the argument? Are we inserting a blank float?
@mudyeet_
@mudyeet_ 2 місяці тому
Are you talking about the f prefix before the quotes? If yes, then that's "f strings"
@pmenown
@pmenown 2 місяці тому
@@mudyeet_ yeah, like what does an 'f string' even mean? Feel like they missed out on calling it a G string
@Lord2225
@Lord2225 3 місяці тому
Fact: You can define custom logic for f'string on your classes and get string after : as argument to __format__
@sasTRproabi
@sasTRproabi 3 місяці тому
What IDE are you using? It looks like VSCode but seems like not
@meowsqueak
@meowsqueak 2 місяці тому
PyCharm
@VypeReaper
@VypeReaper 2 місяці тому
Apart from learning about the f condition, I also learned you can declare the data type in python which i have not been doing lol
@murphygreen8484
@murphygreen8484 3 місяці тому
Don't forget in newer python you can add multiple lines of text by stringing together f strings
5 Good Python Habits
17:35
Indently
Переглядів 291 тис.
5 Tips To Write Better Python Functions
15:59
Indently
Переглядів 66 тис.
5 More Useful F-String Tricks In Python
9:38
Indently
Переглядів 37 тис.
5 Signs of an Inexperienced Self-Taught Developer (and how to fix)
8:40
Travis Media
Переглядів 485 тис.
What is self in Python?
3:29
Python Morsels
Переглядів 22 тис.
All 39 Python Keywords Explained
34:08
Indently
Переглядів 59 тис.
Debugging 101: Replace print() with icecream ic()
12:36
NeuralNine
Переглядів 348 тис.
What is "@total_ordering" in Python?
11:12
Indently
Переглядів 13 тис.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Переглядів 78 тис.
Automate your job with Python
6:07
John Watson Rooney
Переглядів 296 тис.