No video

5 More Useful F-String Tricks In Python

  Рет қаралды 47,506

Indently

Indently

Күн бұрын

Пікірлер: 113
@Indently
@Indently 5 ай бұрын
Hello everyone, another life hack for those of you who find the explanations to be a bit slow, is to set the video speed to something faster like 1.5x or 2x (I edit at 2x usually so watching these feels incredibly slow xD). Otherwise I wish you all a great weekend and happy coding! :)
@alexanderelgert6037
@alexanderelgert6037 5 ай бұрын
If you press the ? key, you will get the shortcuts - my favorites are: j and l ... which jumps 10s and there are the cursor keys left and right which jumps 5s
@Indently
@Indently 5 ай бұрын
I'm learning to "do a KZbin"!
@HenryY8
@HenryY8 2 ай бұрын
How is it to edit at 2x?
@Lukasz72
@Lukasz72 5 ай бұрын
By the way, a better path formatting option is pathlib. For example, it makes sure the slash is correct.
@vorpal22
@vorpal22 5 ай бұрын
You beat me to it... it's a really nice library, and PyCharm gives me warnings for using os.path.join.
@dragweb7725
@dragweb7725 5 ай бұрын
And an even better one is os.path, which processes path strings just like Pathlib but without using a whole Path class
@K0D0R0
@K0D0R0 5 ай бұрын
Or just use forward slashes only lol
@dragweb7725
@dragweb7725 5 ай бұрын
@@K0D0R0 but then your code is not portable on Windows, that's a waste. The os module is built-in python, you have it installed anyway, so why not use it ? Moreover, and most importantly actually, with it you can do much more than just slashing right, you can test the existence and the type of the data pointed by the path, and even create files and directories at given paths automatically from inside your code !
@alexanderelgert6037
@alexanderelgert6037 5 ай бұрын
You can use forward slashes "C:/Windows" or path.join() on an array: >>> import os >>> os.path.join('C:', 'Windows') 'C:/Windows' >>> os.getenv('OS') 'Windows_NT'
@phaedrus2633
@phaedrus2633 5 ай бұрын
It's nice how you keep these topics short and concise. I find myself going catatonic on long videos about the more advanced Python topics.
@M3t4lstorm
@M3t4lstorm 5 ай бұрын
override __format__(format_spec: str) on the class and you can do anything you want, like: f"{something:customformat}"
@dragweb7725
@dragweb7725 5 ай бұрын
Yeah, finally the true advice of them all, any class can benefit from f-strings formatter different ways, you just have to go read the class at the __format__ method to know what's possible
@ego-lay_atman-bay
@ego-lay_atman-bay 5 ай бұрын
The first thing I learned from this video, was that you can use underscores in numbers.
@YPOC
@YPOC 5 ай бұрын
working with jupyter notebooks and lots of paths everyday, I never knew about the fr-string! That will surely come in super handy!
@AWriterWandering
@AWriterWandering 5 ай бұрын
4:26 you can actually still use forward slashes in Windows. Python will automatically do the conversion under the hood.
@bobdeadbeef
@bobdeadbeef 5 ай бұрын
Python isn't doing the conversion. The Windows APIs actually accept forward-slashes! It works everywhere but the native file dialogs and cmd.exe.
@PhrontDoor
@PhrontDoor 3 ай бұрын
NEVER knew about the F and R together thanks so much!
@mrrobotman5299
@mrrobotman5299 5 ай бұрын
Another neat raw string trick is r vs R. Most IDEs will syntax highlight a lower r raw string as regex and an upper R raw string as a pure raw string. Doesn't matter to the interpreter.
@piotrmazgaj
@piotrmazgaj 3 күн бұрын
All of them! Thanks!
@vorpal22
@vorpal22 5 ай бұрын
For trick #3, paths, at this point, I would think people would be using pathlib.Path: from pathlib import Path my_folder = 'python_code' my_path = Path.cwd() / 'scripts' / my_folder which should work regardless of OS.
@airatvaliullin8420
@airatvaliullin8420 5 ай бұрын
agreed! better use something designed for the task instead of tinkering with strings
@viacheslav5710
@viacheslav5710 Ай бұрын
cool explanation, thank you!
@BillyNix
@BillyNix 5 ай бұрын
This was great, I've been using Python for almost 2 decades and I only knew 1 of those tricks.
@marcokraiczek5676
@marcokraiczek5676 5 ай бұрын
The way you explain things is really exceptional. Thank you for your content!
@JohnMitchellCalif
@JohnMitchellCalif 5 ай бұрын
excellent! I've done Python for decades and didn't know all of these. Subscribed.
@krzysiekkrzysiek9059
@krzysiekkrzysiek9059 5 ай бұрын
Awesome. Thx and need more 👌
@rondamon4408
@rondamon4408 5 ай бұрын
Super cool! Looking forward to see the part 3
@juanjosefarina
@juanjosefarina 5 ай бұрын
I saw this video and on the same day the raw string came SO handy ! using f" ... " broke something in my CI/CD, and using fr"" solved it !
@Russet_Mantle
@Russet_Mantle 3 ай бұрын
f-string blows my mind every time I learn more about it
@gunnargu
@gunnargu 5 ай бұрын
Regarding paths just use pathlib.
@robertbogucki9655
@robertbogucki9655 5 ай бұрын
Another good one! My favourite is the one about nesting this time
@robertbogucki9655
@robertbogucki9655 5 ай бұрын
Believe or not but I called my family to say that one of my favourite YT authors loved my comment- You made my day :)
@K0D0R0
@K0D0R0 5 ай бұрын
Sorry, I just got to say it. Firstly, that’s just sad. Secondly, Indently likes most of the comments.
@robertbogucki9655
@robertbogucki9655 5 ай бұрын
@@K0D0R0 small acts of happiness defines our lifes, I am lookint for a reason to smile. Not the opposite, the fact he likes most of his viewers comments makes me happy even more and likes him even more for engaging with community, all the best my friend :)
@Indently
@Indently 5 ай бұрын
Don’t mind him Robert, what he said was absolutely rude and I hope he grows up eventually because that just wasn’t a nice thing to say, and shouldn’t have been mentioned at all. I read every comment and appreciate every comment, I don’t always have time to respond to all of them, so sometimes the best I can do is live a little heart. Thank you for your positive energy Robert :)
@K0D0R0
@K0D0R0 5 ай бұрын
@@Indently truth hurts? Never would have thought. Unless he is 10 y.o., that’s indeed sad. Hope Robert finds actual happiness.
@TheJaguar1983
@TheJaguar1983 5 ай бұрын
I often define a date format at the top of my code to use in strftime. Now with f-strings, this will be much simpler going forward as I uplift code from old servers with out-dated python versions.
@Biokompott
@Biokompott 4 ай бұрын
Really useful!
@AdamAdam-dn4st
@AdamAdam-dn4st 5 ай бұрын
So useful
@nadkoch
@nadkoch 5 ай бұрын
Why are you explicitly define type in initialization? any_var = 'fmt'. In with case type being installed through assignment.
@mudi2000a
@mudi2000a 5 ай бұрын
Regarding the file names, slashes work just fine in Windows. Don’t know if it is an official or unofficial feature though.
@davidagnew8465
@davidagnew8465 5 ай бұрын
Forward slashes work fine in Win32. But they screw up Windows's benighted command interpreter, which uses / as the option introducer (instead of the more civilized hyphen, as in *nix and MacOS).
@mudi2000a
@mudi2000a 5 ай бұрын
@@davidagnew8465 yes that’s right but I use powershell only where slashes also work. It’s very useful when you are using Remote Desktop and have keyboard issues.
@AdamAdam-dn4st
@AdamAdam-dn4st 5 ай бұрын
5:10 rm -rf"{formatcomputer()}"
@bobdeadbeef
@bobdeadbeef 5 ай бұрын
Actually, you don't need to use backslash even on Windows! Forward slash works fine, except in a few limited contexts, like the native file dialogs, or cmd.exe. The filesystem APIs accept forward slash. Also, use the pathlib library rather than constructing strings. The / operator works on Path objects, so you can add levels without string manipulation.
@betterstack
@betterstack 5 ай бұрын
Love the hacks Federico! 🔥If anyone’s looking for more Python tutorials, we’ve released Loguru logging, task scheduling, and more to help the community too 💪
@Pankaj-Verma-
@Pankaj-Verma- 5 ай бұрын
Thanks.
@RedHair651
@RedHair651 5 ай бұрын
I feel like beginners watching this would definitely have benefitted from you explaining that float addition result. It makes no sense if you just pretend it's normal.
@ttrev007
@ttrev007 5 ай бұрын
he has other videos explaining floats
@andrzejostrowski5579
@andrzejostrowski5579 5 ай бұрын
Suggestion for a video topic: when using f-strings is not the best idea. Hint: logging.
@WebPassionist
@WebPassionist 5 ай бұрын
Which IDE are you using?
@elbadrey
@elbadrey 5 ай бұрын
Is that the Python we Know? perfect,... ❤ what pep version you use?
@xuan5306
@xuan5306 5 ай бұрын
Great tips. Thanks. I did run into one interesting issue: say I have a function f(n), and a variable x whose value is 10, and say f(x) returns 20, so I want to print out "f(10)=20", is there anyway to achieve that with f-string in the simplest way?
@Indently
@Indently 5 ай бұрын
f(x=10) could be a possible solution
@MyrLin8
@MyrLin8 5 ай бұрын
awesome!
@rishiraj2548
@rishiraj2548 5 ай бұрын
Thanks
@BobChess
@BobChess 5 ай бұрын
I only .format one
@kumaranb8702
@kumaranb8702 5 ай бұрын
Very nice 🙂❤
@Pawlo370
@Pawlo370 5 ай бұрын
so cool!
@davidmcken
@davidmcken 5 ай бұрын
The embedded formatter from another string just sounds like a security issue waiting to happen.
@haijohemminga5334
@haijohemminga5334 5 ай бұрын
How did you get tthat banana symbol graphic ?
@falklumo
@falklumo 5 ай бұрын
f-strings are a mini language within Python. As are regex and function header docstrings. What other mini-languages do exist within Python? AFAIK, SQL is not one of them. Some consider tuple and dict destructurings another one ;)
@davidagnew8465
@davidagnew8465 5 ай бұрын
Why would you classify regex as a Python minilanguage? Both regex and SQL have an existence completely independent of Python, whereas f-strings are inextricably coupled to the outer language (e.g., the !s and !r format operators being shortcuts to str and repr).
@falklumo
@falklumo 5 ай бұрын
@@davidagnew8465 Well, regex gave rise to r'' "raw" strings and the f'' "formatting" language is ultimatively heritage of Fortran format specifiers. I only abstract and call all of this "mini languages".
@thomaseb97
@thomaseb97 5 ай бұрын
@@falklumolanguage doesnt sound like the best term, regex isn't a language but rather a parser where you provide a parser configuration string it's more like it influences pythons syntax, so maybe python influences is a better term?
@falklumo
@falklumo 5 ай бұрын
@@thomaseb97 regex is a language, nobody would debate that.
@Yvant2000
@Yvant2000 5 ай бұрын
5:14 Ah yes, France-String 🥖🥐
@Neozziee
@Neozziee 5 ай бұрын
What IDE is this?
@adriangrah2917
@adriangrah2917 5 ай бұрын
Is there a way to combine padding around an integer with thousands separators? Something like: print(f"|{next(primes):,_^20}|") Where my intension is to have the _ as the fill character and the number shown using the , as a thousands separator. Wellllp.... never mind. Shortly after asking I tried one more thing..... And this worked: print(f"|{next(primes):_^20,}|")
@henningwilhelmi
@henningwilhelmi 5 ай бұрын
At trick 3, how do i write "{}" as characters in the fr string?
@RedHair651
@RedHair651 5 ай бұрын
You should escape both like this: escaped_curlies = "curly brackets are \{ and \}"
@easypeasy2938
@easypeasy2938 4 ай бұрын
How do you enter emojis in python code? News to me
@michaelhoffmann2891
@michaelhoffmann2891 5 ай бұрын
The saddest part is that when you use Python every day to produce code for your client, like me, you are just down in the rut and don't spend enough time learning these features. Even though they would save you time, make your code better, and long term make you less busy. You're too busy to become less busy! 😭😭😭
@0_-
@0_- 5 ай бұрын
"for real" string
@agentm10
@agentm10 5 ай бұрын
instructions unclear, rm-rf / bombed
@jamesford3282
@jamesford3282 5 ай бұрын
fraw string is smth new for me
@rexygama7697
@rexygama7697 5 ай бұрын
fr (for real)
@vectoralphaSec
@vectoralphaSec 5 ай бұрын
Do you do anything with Django? Im currently trying to learn it and would love to get Django content from you.
@Indently
@Indently 5 ай бұрын
Personally I don't because I hate doing anything that's web dev related. But maybe I'll team up with someone who enjoys doing it in the near future :)
@Fr74n
@Fr74n 5 ай бұрын
@@Indently Yes please!
@mishavuimo5753
@mishavuimo5753 5 ай бұрын
5:11 they try to conceal their experience with the "rf". Poor soul
@MrRahul15937
@MrRahul15937 8 күн бұрын
I think if you are using f , then you don't have to use r
@MichielvanderBlonk
@MichielvanderBlonk 5 ай бұрын
I don't get it. Isn't f'{name !s}' exactly the same as f'{name}'?
@VijayGandhinagar
@VijayGandhinagar 5 ай бұрын
Bob & 'Bob' . !s helps you get rid of the single-quote while printing
@joshix833
@joshix833 5 ай бұрын
​@@VijayGandhinagar!s is the default. !r would add the quotation marks
@MichielvanderBlonk
@MichielvanderBlonk 5 ай бұрын
@VijayGandhinagar print(name) also doesn't show quotes so the !s seems useless for strings.
@VijayGandhinagar
@VijayGandhinagar 5 ай бұрын
Sorry for the confusion. Here is what I was referring to.. give it a try.. name = 'Bob' print(f'{name = }') print(f'{name = !s}') I got .. name = 'Bob' name = Bob '
@ThePrime125
@ThePrime125 5 ай бұрын
If only you could replace the curly brackets with something else... So annoying when putting JavaScript in streamlit or smth
@RedHair651
@RedHair651 5 ай бұрын
If you do this a lot, you definitely should create a JavaScript string class that escapes curly brackets and takes something else as a marker of formatted strings.
@ThePrime125
@ThePrime125 5 ай бұрын
@@RedHair651 Yeah I should do that actually... How exactly you do that never heard about this
@donnillorussia
@donnillorussia 5 ай бұрын
I prefer rf. It makes me feel of Russian Federation
@terryjones9784
@terryjones9784 25 күн бұрын
YOU CAN’T BANANA EMOJI. THERE’S NO BANANA EMOJI IN PYTHON!!!
@Carberra
@Carberra 5 ай бұрын
Bobnana.
@richardbennett4365
@richardbennett4365 5 ай бұрын
Why is 1.62e9 a float. We just saw that it is one billion six hundred twenty million, which is definitely an integer when it is not written in words like I did but rather in numerals.
@affapple3214
@affapple3214 5 ай бұрын
To allow real numbers as well I think You can write like 1.602e-19 which is definitely a real number
@ausarma8403
@ausarma8403 5 ай бұрын
wait... what! he is french?! he must be punished!!
@danielstromberg
@danielstromberg 5 ай бұрын
In Python, "tricks" are generally seen as a Bad thing.
@steelcock
@steelcock 5 ай бұрын
fr!
Python: A Quick Guide To Type Annotations (ft. Mypy)
11:25
Indently
Рет қаралды 30 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 102 М.
managed to catch #tiktok
00:16
Анастасия Тарасова
Рет қаралды 45 МЛН
拉了好大一坨#斗罗大陆#唐三小舞#小丑
00:11
超凡蜘蛛
Рет қаралды 5 МЛН
UNO!
00:18
БРУНО
Рет қаралды 5 МЛН
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 15 МЛН
Pro-Level SwiftUI Animation: StackedSpring Magic! 🚀
3:03
SwiftUI Codes
Рет қаралды 4
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН
10 FORBIDDEN Sorting Algorithms
9:41
Ardens
Рет қаралды 845 М.
How To Use List Comprehension In Python
6:41
Taylor's Software
Рет қаралды 7 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 384 М.
Rotation without rotating.
16:52
Stand-up Maths
Рет қаралды 1 МЛН
Zeno's Paradox - Numberphile
12:05
Numberphile
Рет қаралды 3,4 МЛН
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 265 М.
“typing” is getting deprecated in Python
7:20
Indently
Рет қаралды 17 М.
10 Neat Ways To Use Underscore In Python
18:17
Indently
Рет қаралды 21 М.
managed to catch #tiktok
00:16
Анастасия Тарасова
Рет қаралды 45 МЛН