Python f-strings can do more than you thought. f'{val=}', f'{val!r}', f'{dt:%Y-%m-%d}'

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be talking about f  strings and some of the cool things that you can   do with them. so most of you are probably  already aware of what f strings are. it's   this kind of notation where you can put in f...  little f or capital f, it actually doesn't matter,   and then you have some literal stuff and then  within the string you can put more python code.   so this is really useful and it was introduced  in, i think, python, let's see, python 3.6. but what you may not know is that there's a  lot of extra stuff that you can put in between   these curly braces to modify what actually  gets printed here. so in this first example   we're just printing something out and you  would just see the value is "other dog face"   but one of the really cool things that you  can do is just put an equals sign afterwards,   and then that will actually print out basically  what's before the equal sign, then the equal sign,   and then the value of what's before the equals  sign, so if we go ahead and run this code...   let me go ahead and make this bigger for you...  you can see the first one we just have the value   is "other dog face" and then in the second one  we see string value equals and then the repr   of that variable. so this is extremely useful  especially for debugging purposes if there's   some variable in your code like num_value and i  just want to add a quick debug print statement,   i can just say, oh let me add in a num_value  equals kind of print statement in here and   then immediately you can see it comes  out as num_value equals something,   and that's really useful. i wouldn't really use it  in production code, but especially for debugging   purposes ... you know i end up using it a lot more  than i probably should. another cool thing about   this is that you can actually add spaces basically  anywhere in there and it will preserve the spaces.   so if you really like having, you know, everything  nicely printed out, then you can do that,   put a space before and after in here and  you see it down here when it's printed out.   and this last example you can see that you can  actually have arbitrary expressions there, and so   it will print out, you see, num_value % 2.  that part just gets printed out and then the   value of that expression gets substituted after  the equals sign. so that's a really cool hidden   trick of f strings that you might not have known  about. even though f strings have been in python   since python 3.6, this equals sign ability has  only been in since python 3.8, so if you're not   using a pretty recent version of python then you  might not have access to this yet. but if you do,   that's great, it's super useful. okay next  example, conversions. so if you're not aware,   inside the curly braces of an f string after the  expression you can put a exclamation point a,   exclamation point r, or exclamation point s, and  what these do is instead of printing the value   of this thing, it will additionally do some extra  thing on top of that. so r will call the repr, so   this one right here let's print it out you can see  that it get the quotes in the string printed out   with it and that's because the repr of a string  actually has the quotes in it. so this second one   here is actually equivalent to if i said print the  repr out. the reason that you would want to use   this bang r instead of writing repr out is because  you're probably doing this for debugging purposes.   let's say that thing that you  actually wanted to print out was,   you know, a nice formatted thing "string value"  but something wasn't working quite right so   you say let me just print out the repr so  i can see exactly what's being printed,   and that'll help clear things up. so what--  what's this bang a here? this exclamation   point a? that actually stands for "ascii" so  if you are not familiar with what that does,   it's similar to repr. it's very  similar to repr, except that all of the   basically non-ascii characters get replaced  by an ascii safe escaped version of it, so   this dog face that i have here, you know really  cute but um, if we're limited to just ascii   strings i need to escape that with this backslash  u001f kind of stuff. so in python 3 i don't really   see a big need for doing this kind of thing but  this is more similar to how python 2 reprs worked,   but maybe you just don't want to print out dog  faces because... you know... well i don't know   why anyone wouldn't want to print out a  dog face instead of this nasty thing, but   i guess you can do that. and then of  course your next question is probably:   well you said there was a, r, and s, so what's  the purpose of s? well if you do that it calls   the string conversion operator on the type. and  you might think: well why would i ever want to do   that when the default is to do that? you know,  if i did nothing what it's going to do is call   the string conversion operator and print it out  that way, it's not going to print out the repr,   and the reason that this bang s exists is  because this actually gets applied before   another thing that you can do which is  formatting, and that will be our next   example. so if you're wondering about the  bang s it's for formatting. so if you do one   of these things and then do something that i'm  going to show you in the formatting operator,   it will apply this either string conversion  or repr conversion or ascii conversion and   then apply the formatting after that, so it can  be useful. i usually don't ever do that though.   okay so let's get to the formatting example. here,  so i've got a whole bunch of little examples and   we'll just, you know, see how they all work. so  basically every type can define what it means to   define its own formatting. so i have here, you  know, a float value, and here i have a datetime,   and in my f string i can put a colon and then  format string. so... the format strings are   actually specific to the type, so the things that  i can put here for a datetime are different than   the things that i put here for a numeric value,  so you can see here if i have a datetime i can   print it out year month day like that, and that'll  work. so i'll go ahead and run it and you can see   now equals 2021 06 14. so that format  string works for datetimes and then   for, you know, numeric values. this .2f is  actually a format string telling me i want   two decimal places. so you don't have to  manually round things, you can just say i   want two decimal places and then the formatting  library, or the formatting function within   the number class that you're using, will handle  the rounding and displaying part for you.   so what's actually going on under the hood here is  a little bit easier to see if you define your own   class. so here i've defined this MyClass and let's  see what it does. it's just an empty class and all   it does is define a format method which takes a  format spec and is supposed to return a string.   so let's see what happens when we use our  own class in one of these format, you know,   things and see what actually happens when it  gets called. so when i go ahead and run it,   you can see that i see my class dunder format  called with format_spec equals blah blah blah   so you can see this blah blah my format stuff is  exactly what was after the colon inside of the f   string in the curly braces. so that is getting  passed as an argument the format_spec argument   to the dunder format method of the class, and you  can do whatever you want with that information. so   it's totally up to you. in this case i just, you  know, totally ignored the information and just   always return MyClass(). this is a way to  allow your own class to decide what are some   special ways that i can be printed out. so  that's not something that i do super often,   but it's kind of more important that you just  know that that's what the syntax is doing.   basically whatever you pass here is just  getting passed to a special function   of the class. and so you can look up each class  in the documentation is going to tell you what its   format strings are. and you know how to make use  of them so especially for numbers this, you know,   automatically rounding to two decimal places, i  do that all the time. so just be aware that that's   another thing. all right well that's all i've  got on f strings and little f string tricks. i   hope you enjoyed this little more informal video  and i just wanted to give a quick shout out to   my patrons on patreon, thank you guys so much i  really appreciate your support see you next time.
Info
Channel: mCoding
Views: 101,477
Rating: 4.9750614 out of 5
Keywords: python, format strings, f-strings, python string formatting
Id: BxUxX1Ku1EQ
Channel Id: undefined
Length: 9min 10sec (550 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.