Тёмный

5 Useful F-String Tricks In Python 

Indently
Подписаться 181 тыс.
Просмотров 260 тыс.
50% 1

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

Опубликовано:

 

10 фев 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 328   
@chyldstudios
@chyldstudios 3 месяца назад
the "=" trick in the f-string is fire.
@m-zurowski
@m-zurowski 3 месяца назад
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 3 месяца назад
ok, my idea didn't work as expected 😅
@dark_brownie
@dark_brownie Месяц назад
Yeah, completely agree it is amazing
@sunwukong6268
@sunwukong6268 4 месяца назад
Ever since I learned f-strings...I love them.
@GentleMannOfHats
@GentleMannOfHats 3 месяца назад
I wish I had known sooner!!
@rickymort135
@rickymort135 3 месяца назад
And I love you.... Sorry if I made things awkward... 😬 Oof this awkward isn't it?
@andymitchell2146
@andymitchell2146 3 месяца назад
I've been using python for about 10 years, and f strings extensively, but never knew that last tip! Game changer!
@yash1152
@yash1152 3 месяца назад
well, thats cause its only a very recent one (afaik)
@eyehear10
@eyehear10 2 месяца назад
@@yash1152it was introduced in 3.8, so a while back
@Zenivo
@Zenivo 3 месяца назад
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.
@_Loki__Odinson_
@_Loki__Odinson_ 4 месяца назад
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 4 месяца назад
That's the hype for type annotations that I love to see!
@Naej7
@Naej7 4 месяца назад
Not using type annotations should be banned by law
@_Loki__Odinson_
@_Loki__Odinson_ 4 месяца назад
@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 4 месяца назад
@@_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 4 месяца назад
​@@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? 🤦‍♂️
@utarasama
@utarasama 4 месяца назад
The last one is super duper cool!
@dcx45
@dcx45 3 месяца назад
RIP the maintainer
@utarasama
@utarasama 3 месяца назад
​@@dcx45 he should then watch this video
@BohumirZamecnik
@BohumirZamecnik Месяц назад
Very nice. Another useful is formatting float as percent: f"{foo:.2%}".
@TheJaguar1983
@TheJaguar1983 4 месяца назад
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.
@krzysiekkrzysiek9059
@krzysiekkrzysiek9059 4 месяца назад
This kind of tips are awesone. We need more 👍
@rolandsz8831
@rolandsz8831 3 месяца назад
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"
@TheMcSebi
@TheMcSebi 4 месяца назад
Great video! Didn't know about the datetime and debug print ones. Definitely going to use them in the future, though.
@jaa928
@jaa928 3 месяца назад
Thank you for the instructive tips!
@quekki3666
@quekki3666 4 месяца назад
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 4 месяца назад
It's a cool trick for sure!
@bashar9200
@bashar9200 3 месяца назад
This is amazing!! thank you for this tutorial!!
@richsadowsky8580
@richsadowsky8580 3 месяца назад
Absolutely fantastic and useful video!
@dipeshsamrawat7957
@dipeshsamrawat7957 4 месяца назад
Nice collection! 💯
@SergioYT2052
@SergioYT2052 3 месяца назад
"Simple y bello como un anillo", como diría Neruda; pero además, muy funcional. ¡Muchas gracias!
@mattshu
@mattshu 4 месяца назад
F strings are soo chef kiss
@timegor844
@timegor844 4 месяца назад
Wow, so many simple things I didn't about... Thank you
@kychemclass5850
@kychemclass5850 3 месяца назад
Love #5. Thank you.
@flashtrack101
@flashtrack101 3 месяца назад
Love your vids man! would love to see a tutorial on cython from you!
@viniciomonge3960
@viniciomonge3960 2 месяца назад
Loved the last trick!!!
@UndyingEDM
@UndyingEDM 2 месяца назад
For those who didn't know, the last one is called self-documenting expression and was released in Python 3.8
@Pawlo370
@Pawlo370 4 месяца назад
Finaly! All fstring variations in one film
@mjhaynavarro
@mjhaynavarro 2 месяца назад
So cool.. thanks for sharing it. very informative
@ChrisHalden007
@ChrisHalden007 3 месяца назад
Great video. Thanks
@griffgruff1
@griffgruff1 3 месяца назад
Great video!
@rahulCoding
@rahulCoding 4 месяца назад
Great video.... Thanks a lot😍👍
@catastrophicblues13
@catastrophicblues13 3 месяца назад
That last one is sooo useful!
@karthikkarthik100
@karthikkarthik100 4 месяца назад
Last trick was super cool...
@cmcdougle
@cmcdougle 3 месяца назад
Thank you, this went STRAIGHT into my current project. Commas in numbers was one of the next things I was going to look up.
@Anzeljaeg
@Anzeljaeg 3 месяца назад
This is pure gold 🥇
@MahdiImeni
@MahdiImeni 4 месяца назад
Loved it ❤
@TheKahunas2722000
@TheKahunas2722000 3 месяца назад
Love the video I knew some of those but the last one is epic I will be using that from now on .
@sidjay7644
@sidjay7644 4 месяца назад
Very nice Thanks
@alisajjad2478
@alisajjad2478 22 дня назад
Thanks for the tips. The last trick is amazing
@swolekhine
@swolekhine 4 месяца назад
These will be useful to me for sure. Here's a comment for the algorithm gods!
@SaveCount-bh8tp
@SaveCount-bh8tp 4 месяца назад
Thanks very much
@mrjamesflores
@mrjamesflores 4 месяца назад
The last one was good!
@Angelinajolieshorts
@Angelinajolieshorts 3 месяца назад
Great work sir❤
@LittleGnawer
@LittleGnawer 3 месяца назад
Nice and useful tricks for every day programming. I also prefer specifying types of variables, since it makes code better understandable.
@dark-ghost4132
@dark-ghost4132 4 месяца назад
Thank for nice tricks 😘
@artistpw
@artistpw 2 месяца назад
Very nice.
@kinngrimm
@kinngrimm 3 месяца назад
thanks for the showcases
@cheesy_boya
@cheesy_boya 4 месяца назад
Yep, I'm saving this video
@tolgaflashtr2855
@tolgaflashtr2855 4 месяца назад
too* 😂😂
@Krullfath
@Krullfath 4 месяца назад
This is super cool, I sadly can't think of any usecases in my current project
@adeptusmortem
@adeptusmortem 3 месяца назад
Thank you
@Little-bird-told-me
@Little-bird-told-me Месяц назад
Good Job
@Sailesh_Bhoite
@Sailesh_Bhoite 3 месяца назад
Nice Tricks!
@aliwalil4160
@aliwalil4160 4 месяца назад
the last fstring was dope
@mad_vegan
@mad_vegan 4 месяца назад
To use scientific notation with integers, you can either do int(2e9) or 2*10**9.
@aguy9836
@aguy9836 4 месяца назад
nice examples
@ErLakhan
@ErLakhan 2 месяца назад
Superb
@oldschoolsoldier1634
@oldschoolsoldier1634 4 месяца назад
Trick #2 is neat
@daveys
@daveys 4 месяца назад
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 4 месяца назад
Earlier I didn't know it worked on whole expressions, I think it's super cool as well!
@Andrey_Fedorov
@Andrey_Fedorov 3 месяца назад
Круто! Спасибо!
@chrisogonas
@chrisogonas 3 месяца назад
Awesome! I love the '=' and >
@yash1152
@yash1152 3 месяца назад
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.
@MechanicusOfficial
@MechanicusOfficial 2 месяца назад
I always use pyformat. Very easy to understand and pretty nice too. Var = 15 print(“this is my var: {}”.format(var))
@richardnicholas2957
@richardnicholas2957 3 месяца назад
Gold!
@noir66146
@noir66146 3 месяца назад
ooh i like these videos logic magic !
@gopal.
@gopal. 2 месяца назад
thanks
@Al_Miqdad_
@Al_Miqdad_ 4 месяца назад
thanks for your time please make videos about data structure
@GW-nh9qc
@GW-nh9qc 2 месяца назад
Klasse 😃👍
@meghanelizondo774
@meghanelizondo774 3 месяца назад
I was so stoked when f'{ var = }' was added to Python!! Might be abusing it a bit lol
@-george-1153
@-george-1153 2 месяца назад
You're cool! 👍
@Mor3Lif3
@Mor3Lif3 2 месяца назад
That last one goes wild
@casperghst42
@casperghst42 2 месяца назад
Intersting, a large part of the world is using . (period) as a 1000 separator and , (comma) as a decimal point.
@user-hd3pz2ow1b
@user-hd3pz2ow1b 3 месяца назад
rounding in python .. cool
@kinngrimm
@kinngrimm 3 месяца назад
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.
@NickCombs
@NickCombs 3 месяца назад
This definitely seems more convenient than it is in js.
@semenivanoff8615
@semenivanoff8615 2 месяца назад
Useful. Thanks
@sayantanguha1934
@sayantanguha1934 3 месяца назад
The last one blew my mind
@murphygreen8484
@murphygreen8484 4 месяца назад
Don't forget in newer python you can add multiple lines of text by stringing together f strings
@archiemarqx
@archiemarqx 4 месяца назад
3:24 this tip will literally improve my code quality
@fg786
@fg786 4 месяца назад
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.
@Lord2225
@Lord2225 4 месяца назад
Fact: You can define custom logic for f'string on your classes and get string after : as argument to __format__
@kinngrimm
@kinngrimm 3 месяца назад
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)
@0MVR_0
@0MVR_0 4 месяца назад
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.
@NC700xLover
@NC700xLover 3 месяца назад
Which editor is that? Looks fresh
@brycesakal3717
@brycesakal3717 Месяц назад
My professor who I took intro to Python called the “” ‘right justify and left justify’
@acherongoon
@acherongoon 4 месяца назад
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'.
@xKiiyoshiix
@xKiiyoshiix 4 месяца назад
Hello @Indently, Can you please explain me, why you use ":" after a variable for ex. n:? Regards.
@matthewbay1978
@matthewbay1978 4 месяца назад
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?
@veeratheking8375
@veeratheking8375 4 месяца назад
big fan bro from india
@Indently
@Indently 4 месяца назад
Thanks!
@iscatafan2959
@iscatafan2959 4 месяца назад
You make the code easier to read, so you better use the functions of the str class! 🐳
@anon_y_mousse
@anon_y_mousse 3 месяца назад
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.
@annieshedden1245
@annieshedden1245 Месяц назад
i'm old but i still think everyone should know that most of the f-string stuff comes from C printf/strftime/etc.
@rutenowynidoking3593
@rutenowynidoking3593 4 месяца назад
For debugging, I recommend the "Icecream" library.
@tudaer
@tudaer 3 месяца назад
May I ask which IDE and development env are you using? Looks so great
@meowsqueak
@meowsqueak 3 месяца назад
It’s PyCharm
@tudaer
@tudaer 3 месяца назад
@@meowsqueak thanks!
@eugenepark
@eugenepark 7 дней назад
Is it possible to combine f"{n:,}" and f"{n:20}" somehow? or are they an exclusive or
@MkReman
@MkReman 4 месяца назад
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 3 месяца назад
It’s the new UI. Perhaps you’re still using the legacy UI?
@Carberra
@Carberra 3 месяца назад
That last tip is the biggest argument against a debugger; if they wanted you to use it, why did they provide that debug syntax? 🤓
@Alchemist10241
@Alchemist10241 Месяц назад
1e9 was the most useful to me
@hlubradio2318
@hlubradio2318 2 месяца назад
Wow
@kamurashev
@kamurashev 4 месяца назад
Yep, f string is a great feature. At least something Python gives us 😅
@VypeReaper
@VypeReaper 3 месяца назад
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 3 месяца назад
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 2 месяца назад
Probably added in a later version. I'd love to know which. Edit: it's called self-documenting expression released in Python 3.8
@shabadooshabadoo4918
@shabadooshabadoo4918 4 месяца назад
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 4 месяца назад
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 4 месяца назад
@@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.
@jameswise9171
@jameswise9171 3 месяца назад
So much better than Rust's format! macro
@andrewmalani1882
@andrewmalani1882 2 месяца назад
what complier/interpreter does he use?
Далее
5 Good Python Habits
17:35
Просмотров 366 тыс.
5 Uncommon Python Features I Love
15:09
Просмотров 130 тыс.
5 More Useful F-String Tricks In Python
9:38
Просмотров 41 тыс.
5 Useful Dunder Methods In Python
16:10
Просмотров 51 тыс.
Python Generators Explained
28:37
Просмотров 144 тыс.
Debugging 101: Replace print() with icecream ic()
12:36
5 Useful Python Decorators (ft. Carberra)
14:34
Просмотров 85 тыс.
11 Tips And Tricks To Write Better Python Code
11:00
Просмотров 601 тыс.
5 Really Cool Python Functions
19:58
Просмотров 42 тыс.
Python: A Quick Guide To Type Annotations (ft. Mypy)
11:25
Please Master These 10 Python Functions…
22:17
Просмотров 43 тыс.