5 Useful F-String Tricks In Python

  Рет қаралды 329,245

Indently

Indently

Күн бұрын

Пікірлер: 357
@chyldstudios
@chyldstudios 9 ай бұрын
the "=" trick in the f-string is fire.
@m-zurowski
@m-zurowski 9 ай бұрын
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 9 ай бұрын
ok, my idea didn't work as expected 😅
@dark_brownie
@dark_brownie 6 ай бұрын
Yeah, completely agree it is amazing
@Zenivo
@Zenivo 9 ай бұрын
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.
@sunwukong6268
@sunwukong6268 9 ай бұрын
Ever since I learned f-strings...I love them.
@GentleMannOfHats
@GentleMannOfHats 9 ай бұрын
I wish I had known sooner!!
@rickymort135
@rickymort135 9 ай бұрын
And I love you.... Sorry if I made things awkward... 😬 Oof this awkward isn't it?
@andymitchell2146
@andymitchell2146 9 ай бұрын
I've been using python for about 10 years, and f strings extensively, but never knew that last tip! Game changer!
@yash1152
@yash1152 9 ай бұрын
well, thats cause its only a very recent one (afaik)
@eyehear10
@eyehear10 8 ай бұрын
@@yash1152it was introduced in 3.8, so a while back
@_Loki__Odinson_
@_Loki__Odinson_ 9 ай бұрын
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 9 ай бұрын
That's the hype for type annotations that I love to see!
@Naej7
@Naej7 9 ай бұрын
Not using type annotations should be banned by law
@_Loki__Odinson_
@_Loki__Odinson_ 9 ай бұрын
@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 9 ай бұрын
@@_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 9 ай бұрын
​@@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? 🤦‍♂️
@BohumirZamecnik
@BohumirZamecnik 7 ай бұрын
Very nice. Another useful is formatting float as percent: f"{foo:.2%}".
@utarasama
@utarasama 9 ай бұрын
The last one is super duper cool!
@dcx45
@dcx45 9 ай бұрын
RIP the maintainer
@utarasama
@utarasama 9 ай бұрын
​@@dcx45 he should then watch this video
@rolandsz8831
@rolandsz8831 8 ай бұрын
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"
@SergioYT2052
@SergioYT2052 9 ай бұрын
"Simple y bello como un anillo", como diría Neruda; pero además, muy funcional. ¡Muchas gracias!
@TheJaguar1983
@TheJaguar1983 9 ай бұрын
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.
@enriqueDFTL
@enriqueDFTL 5 ай бұрын
I never use anything other than f-strings when printing and these tips are great. Going to use them!
@krzysiekkrzysiek9059
@krzysiekkrzysiek9059 9 ай бұрын
This kind of tips are awesone. We need more 👍
@im_a_surfingdoggo
@im_a_surfingdoggo 9 ай бұрын
Thank you, this went STRAIGHT into my current project. Commas in numbers was one of the next things I was going to look up.
@mattshu
@mattshu 9 ай бұрын
F strings are soo chef kiss
@TheMcSebi
@TheMcSebi 9 ай бұрын
Great video! Didn't know about the datetime and debug print ones. Definitely going to use them in the future, though.
@itsyaboyceez
@itsyaboyceez Ай бұрын
Python pointing out you’re high. It’s gonna be a game changer when that module drops 😂😂😂 3:50
@mad_vegan
@mad_vegan 9 ай бұрын
To use scientific notation with integers, you can either do int(2e9) or 2*10**9.
@UndyingEDM
@UndyingEDM 7 ай бұрын
For those who didn't know, the last one is called self-documenting expression and was released in Python 3.8
@KinkyJalepeno
@KinkyJalepeno 5 ай бұрын
This series of vids are the best on youtube - keep it going please, best sub ever.
@SobTim-eu3xu
@SobTim-eu3xu 5 ай бұрын
First is fire Second is fire(unless filling, I know this) Third is like in C#, but without f string Fourth I know, and also like in C#(unless ,.3f, this is fire) Fifth is fire, this is why I live python) 1year+ of python) I use it in cryptography, and numerical methods)
@daveys
@daveys 9 ай бұрын
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 9 ай бұрын
Earlier I didn't know it worked on whole expressions, I think it's super cool as well!
@alfredo.onyoutube
@alfredo.onyoutube Ай бұрын
Didn't know the last one! Thanks 👍
@MechanicusOfficial
@MechanicusOfficial 7 ай бұрын
I always use pyformat. Very easy to understand and pretty nice too. Var = 15 print(“this is my var: {}”.format(var))
@mjhaynavarro
@mjhaynavarro 7 ай бұрын
So cool.. thanks for sharing it. very informative
@richsadowsky8580
@richsadowsky8580 9 ай бұрын
Absolutely fantastic and useful video!
@LittleGnawer
@LittleGnawer 9 ай бұрын
Nice and useful tricks for every day programming. I also prefer specifying types of variables, since it makes code better understandable.
@cheesy_boya
@cheesy_boya 9 ай бұрын
Yep, I'm saving this video
@tolgaflashtr2855
@tolgaflashtr2855 9 ай бұрын
too* 😂😂
@karthikkarthik100
@karthikkarthik100 9 ай бұрын
Last trick was super cool...
@quekki3666
@quekki3666 9 ай бұрын
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 9 ай бұрын
It's a cool trick for sure!
@viniciomonge3960
@viniciomonge3960 8 ай бұрын
Loved the last trick!!!
@jaa928
@jaa928 8 ай бұрын
Thank you for the instructive tips!
@catastrophicblues13
@catastrophicblues13 9 ай бұрын
That last one is sooo useful!
@flashtrack101
@flashtrack101 9 ай бұрын
Love your vids man! would love to see a tutorial on cython from you!
@yash1152
@yash1152 9 ай бұрын
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.
@alisajjad2478
@alisajjad2478 6 ай бұрын
Thanks for the tips. The last trick is amazing
@timegor844
@timegor844 9 ай бұрын
Wow, so many simple things I didn't about... Thank you
@TheKahunas2722000
@TheKahunas2722000 9 ай бұрын
Love the video I knew some of those but the last one is epic I will be using that from now on .
@Pawlo370
@Pawlo370 9 ай бұрын
Finaly! All fstring variations in one film
@Anzeljaeg
@Anzeljaeg 9 ай бұрын
This is pure gold 🥇
@bashar9200
@bashar9200 9 ай бұрын
This is amazing!! thank you for this tutorial!!
@casperghst42
@casperghst42 7 ай бұрын
Intersting, a large part of the world is using . (period) as a 1000 separator and , (comma) as a decimal point.
@acherongoon
@acherongoon 9 ай бұрын
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'.
@asakhhh
@asakhhh Ай бұрын
adding to the note that only underscores and commas can be used: I think we can just apply character replacing to the resulting string
@archiemarqx
@archiemarqx 9 ай бұрын
3:24 this tip will literally improve my code quality
@kychemclass5850
@kychemclass5850 9 ай бұрын
Love #5. Thank you.
@Lord2225
@Lord2225 9 ай бұрын
Fact: You can define custom logic for f'string on your classes and get string after : as argument to __format__
@anon_y_mousse
@anon_y_mousse 9 ай бұрын
I wish more languages would copy this feature, and that Python would allow you to use variables inside the strings to select formatting options. For my own language, I made all strings f-strings and just have a shorthand where "$var" will stringify the value of `var`, and "${ expr }" will stringify the value of any valid expression. I made it so that format specifiers could use a variable instead of a numeric constant in the string. That way you could pad the output to the terminal dependent on the actual terminal attributes without using a loop. I'm hoping that this methodology negates the need for *printf() functions in my language, because separating the variable from any formatting options on it is error prone, and most of the time people just want to print as is.
@elbadrey
@elbadrey 4 ай бұрын
Honestly l 💖 your style explanation, and knowledge. How do you get this information, what books you read, or perfect roadmap with reference to follow.
@ChrisHalden007
@ChrisHalden007 9 ай бұрын
Great video. Thanks
@murphygreen8484
@murphygreen8484 9 ай бұрын
Don't forget in newer python you can add multiple lines of text by stringing together f strings
@eloyam9973
@eloyam9973 2 ай бұрын
The last one was 🔥
@meghanelizondo774
@meghanelizondo774 9 ай бұрын
I was so stoked when f'{ var = }' was added to Python!! Might be abusing it a bit lol
@0MVR_0
@0MVR_0 9 ай бұрын
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.
@aliwalil4160
@aliwalil4160 9 ай бұрын
the last fstring was dope
@rahulCoding
@rahulCoding 9 ай бұрын
Great video.... Thanks a lot😍👍
@sidjay7644
@sidjay7644 9 ай бұрын
Very nice Thanks
@Krullfath
@Krullfath 9 ай бұрын
This is super cool, I sadly can't think of any usecases in my current project
@vedantkanoujia
@vedantkanoujia 6 ай бұрын
you helped me for spaces , I used it in list with \t
@NickCombs
@NickCombs 9 ай бұрын
This definitely seems more convenient than it is in js.
@Angelinajolieshorts
@Angelinajolieshorts 9 ай бұрын
Great work sir❤
@swolekhine
@swolekhine 9 ай бұрын
These will be useful to me for sure. Here's a comment for the algorithm gods!
@dipeshsamrawat7957
@dipeshsamrawat7957 9 ай бұрын
Nice collection! 💯
@griffgruff1
@griffgruff1 9 ай бұрын
Great video!
@chrisogonas
@chrisogonas 9 ай бұрын
Awesome! I love the '=' and >
@mrjamesflores
@mrjamesflores 9 ай бұрын
The last one was good!
@SaveCount-bh8tp
@SaveCount-bh8tp 9 ай бұрын
Thanks very much
@xKiiyoshiix
@xKiiyoshiix 9 ай бұрын
Hello @Indently, Can you please explain me, why you use ":" after a variable for ex. n:? Regards.
@matthewbay1978
@matthewbay1978 9 ай бұрын
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?
@alex_chugaev
@alex_chugaev 4 ай бұрын
Honestly, adding that much syntactic sugar to programming language only makes it worse, not better. I’d prefer pure functions with clear names which do the work. Such code would be easier to maintain.
@kinngrimm
@kinngrimm 9 ай бұрын
thanks for the showcases
@MahdiImeni
@MahdiImeni 9 ай бұрын
Loved it ❤
@nuynobi
@nuynobi 5 ай бұрын
Only the last one is specific to f-strings though, right? The rest are all just general string formatting techniques. Good tips nonetheless.
@itsmeashbeel9175
@itsmeashbeel9175 2 ай бұрын
No I believe you need to use fstring for all examples. You can tell by the use of code in curly brackets. In other strings the curly brackets won't do anything code related
@nuynobi
@nuynobi 2 ай бұрын
@@itsmeashbeel9175 Negatory. He demonstrates them using fstrings but they need not be. All but one of these tips can be used to format a regular string with its `format` method (ie 'new style' string formatting which uses curly braces to indicate replacements, just like fstrings do).
@cktan0
@cktan0 Ай бұрын
Tip 5 should be tip 1
@yelircaasi
@yelircaasi 8 ай бұрын
Which editor is that? Looks fresh
@sayantanguha1934
@sayantanguha1934 8 ай бұрын
The last one blew my mind
@dark-ghost4132
@dark-ghost4132 9 ай бұрын
Thank for nice tricks 😘
@kinngrimm
@kinngrimm 9 ай бұрын
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)
@artistpw
@artistpw 7 ай бұрын
Very nice.
@Al_Miqdad_
@Al_Miqdad_ 9 ай бұрын
thanks for your time please make videos about data structure
@tudaer
@tudaer 9 ай бұрын
May I ask which IDE and development env are you using? Looks so great
@meowsqueak
@meowsqueak 9 ай бұрын
It’s PyCharm
@tudaer
@tudaer 9 ай бұрын
@@meowsqueak thanks!
@Mor3Lif3
@Mor3Lif3 8 ай бұрын
That last one goes wild
@kinngrimm
@kinngrimm 9 ай бұрын
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.
@oldschoolsoldier1634
@oldschoolsoldier1634 9 ай бұрын
Trick #2 is neat
@VypeReaper
@VypeReaper 9 ай бұрын
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
@ArtyomKatsap
@ArtyomKatsap 9 ай бұрын
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 7 ай бұрын
Probably added in a later version. I'd love to know which. Edit: it's called self-documenting expression released in Python 3.8
@Carberra
@Carberra 9 ай бұрын
That last tip is the biggest argument against a debugger; if they wanted you to use it, why did they provide that debug syntax? 🤓
@fg786
@fg786 9 ай бұрын
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.
@adeptusmortem
@adeptusmortem 8 ай бұрын
Thank you
@brycesakal3717
@brycesakal3717 6 ай бұрын
My professor who I took intro to Python called the “” ‘right justify and left justify’
@noir66146
@noir66146 8 ай бұрын
ooh i like these videos logic magic !
@Sailesh_Bhoite
@Sailesh_Bhoite 8 ай бұрын
Nice Tricks!
@bitelogger
@bitelogger 4 ай бұрын
Hi, please which IDE are you using on this video?
@iscatafan2959
@iscatafan2959 9 ай бұрын
You make the code easier to read, so you better use the functions of the str class! 🐳
@rutenowynidoking3593
@rutenowynidoking3593 9 ай бұрын
For debugging, I recommend the "Icecream" library.
@Andrey_Fedorov
@Andrey_Fedorov 9 ай бұрын
Круто! Спасибо!
@annieshedden1245
@annieshedden1245 6 ай бұрын
i'm old but i still think everyone should know that most of the f-string stuff comes from C printf/strftime/etc.
@aguy98ptk
@aguy98ptk 9 ай бұрын
nice examples
@Little-bird-told-me
@Little-bird-told-me 6 ай бұрын
Good Job
@neilmeich
@neilmeich 9 ай бұрын
rounding in python .. cool
@kinngrimm
@kinngrimm 9 ай бұрын
in the first example, do you know what decimal points wouldn't work?
@andrewmalani1882
@andrewmalani1882 7 ай бұрын
what complier/interpreter does he use?
5 Good Python Habits
17:35
Indently
Рет қаралды 623 М.
How To Write Better Functions In Python
14:17
Indently
Рет қаралды 49 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 12 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 98 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 57 МЛН
5 More Useful F-String Tricks In Python
9:38
Indently
Рет қаралды 52 М.
5 Python Libraries You Should Know in 2025!
22:30
Keith Galli
Рет қаралды 20 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 107 М.
Python's 5 Worst Features
19:44
Indently
Рет қаралды 111 М.
8 Data Structures Every Programmer Should Know
17:09
ForrestKnight
Рет қаралды 174 М.
All 39 Python Keywords Explained
34:08
Indently
Рет қаралды 208 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 301 М.
threading vs multiprocessing in python
22:31
Dave's Space
Рет қаралды 593 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 111 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 12 МЛН