5 More Useful F-String Tricks In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video was brought to you by IND dentle IO learning python Made Simple how's it going everyone in today's video we're going to be exploring five more useful fstring tricks that you can use in Python and the reason I'm making this video is because you guys really loved the previous one and if you haven't seen the previous one I've left a link to it in the description box down below which I absolutely recommend you check out before watching this video because I'm going to be using a lot of the tricks from the previous video to explain how these ones work anyway for trick number one we're going to pretend we have a huge number and we're just going to call that big number and this big number is going to equal 1 b620 million so it's quite a big number and yes you can also write that out in scientific notation but suppose you're getting it back from an API or from some sort of calculation when you print this number it's going to to display the whole number inside the console but something cool you can do instead is specify to be in the format of scientific notation and you can do that just by providing an e and like this it still looks just as ugly it still takes just as many spaces as if we were to see the entire number so something that's even more useful than that is specifying how many decimal places we want to use with this scientific notation for example we can type in 2 e and then it will give us back this scientific notation which in Python you can write out as 1.62 e to the 9th and that will give us the exact same result except that would have to be a float moving on to trick number two and for this example I'm going to import from date time date time so that we can fetch the current date and time and that's going to equal daytime do now and originally in the previous video I showed you that we could format this datetime object using a datetime specifier so here we could insert let's say the day dot the month dot the year and if we were to run that we would get our date formatted according to our specifications but one thing I didn't show you is that we can actually insert a variable here instead of hardcoding the string for example maybe we have a different specifier we want to use or maybe we have several specifiers that we want to use so one nice thing we can do is create a variable and store this inside that variable then you're probably thinking this is quite straightforward all we need to do is insert the date time specifier here but that's not going to work of course because this is where we insert our FST string specifiers so it's not going to understand that this is supposed to be a variable but one thing that's quite silly that you can do is Nest your FST strings so that you can use a variable as a format specifier and now we can edit this from anywhere so if we were to run this we will get the exact same result and you can do this with any specifier and as another example I created a huge number which is quite ugly with a lot of decimal places and a lot of zeros and here I decided to create a specifier of comma. 2f so that's going to round our number to two decimal places and it's going to insert a th000 separator for each thousand and as you could see just like with the date I was able to insert it as a variable using some nested parentheses so now if we were to run this we would get this formatted number trick number three in Python it can be quite tricky to refer to a file path and that's because some file paths require us to use backslashes which in Python also work as an escape and that means that it might Escape something as a uni code character it might Escape something using that escape code syntax and you never know where this is going to happen I mean if you know how escaping works you do know where this is going to happen but you just don't want this to happen in your path so one of the very first things we learn in Python is to create a raw string if we're going to use a file path and to do that we just need to insert an r in front of our string and then we can print that path without any issues and I believe the backs slash is mostly used on Windows because as you can see on Mac everything uses a forward slash but that's not the point the point is that sometimes you're going to to have backs slashes that escape characters that you do not want to escape in your strings but something really nice to know is that you can combine these raw strings with f strings for example maybe we don't want to use a random folder maybe we want to dynamically update that with a new folder name each time we run the script and to do so I'm going to go above that and create something called custom folder which will be of typ string and it's going to equal indent the most important folder on my computer and to to make this work with FST strings all we need to do is insert an F in front of the r or after the r and that will allow you to use both the raw string and the F string together personally I like to use f in front of R because it just makes me think of fronts but you can pick whatever combination you prefer but once you've done that you can insert your custom folder and the next time we try to print this path we should get our custom folder printed up next we have trick number four and for this example I'm going to create two variables one called a of type float which has the value of 0.1 and another one called B which is also of type float with the value of 0 2 and what we're going to do with both of these is print the sum which by this point if you've been programming for quite a while you'll know is going to give us back the most ugly number ever and just to demonstrate it we're going to add a plus b and we're going to use that quick debugging syntax so now when we run this we should get a result such as this one and we learned this in the previous video but one thing I did not show you in the previous video is that you can combine the quick debugging syntax with format specifiers so if we want to format the result we can add a colon and we can do something such as 0.1f and the next time we run this it's actually going to format it according to what we inserted as a format specifier and that's really cool because now we have more control over what our FST string can do and just to show you one more example I'm going to create a random name called Bob and here we're going to print that the name is equal to and I'm going to use the exclamation mark and S and all this is saying is that this should be interpreted as a string because by default when you are using this syntax it prints the representation but by using this syntax we're telling python that you should print it as a string so as you can see the name is now equal to Bob without this we're going to get the representation so the name is equal to the string of Bob and the string of Bob might be much more descriptive so personally I would probably leave it at that but it's good to know that in case you don't want that you can change it back to a string just to get Bob as an output and now it's time for the final trick and this is something really cool that I just learned today so for this example I'm going to import from date time the date and time and the date then I'm going to create a banana of type string which is going to be the banana emoji and below that I will create a name which is completely random called Bob and I'm going to get today's date which is going to be of type date and that's going to equal date time do now. dat and now that we have these three variables we can print first the date and I'm also going to include this special syntax which again converts whatever variable we're using to its string form and then we're going to say that the name with the exclamation s once again says the most important phrase in the world which is banana which will once again contain the string format specifier and this should actually be around the date and next we're going to duplicate this two times because what I want to do here is change this to an r and for the final one we're going to change it to an a just so we can compare these three and that's supposed to be an R how dare you you infiltrate that line a and actually before we run this I made a major mistake and that is to insert the date instead of today but now let's run our script and analyze the result so I'll just scroll up a bit so you can see these as well and for our first print statement we get the string representation for each one of our variables so everything prints as normal we get today's date and we get that Bob says banana for the second print statement we are printing the representation of each variable so here we get the datetime object we get the string of Bob and the string of banana and for the final one we get the ask key representation which means the daytime object will just be printed as follows because there's not really an asky representation for that and neither is there one for Bob but for banana we're going to get it printed as a Unicode so that's just some other really cool syntax that you can use with FST strings but anyways those were the five fing formatting tricks that I wanted to bring up today do let me know in the comment section down below which ones were your favorites but otherwise as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 44,279
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: aa39jL7wdJs
Channel Id: undefined
Length: 9min 37sec (577 seconds)
Published: Fri Mar 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.