5 Useful F-String Tricks In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everyone in this video we're going to be covering five cool F string formatting tricks that you can use in Python so let's get started immediately with the first trick and in this example I'm going to create an integer called n and that's going to be some huge number now at this point this is quite illegible so by now you probably know that you can already insert underscores to make them slightly more readable and that this will be ignored by the interpreter which means that if we were to print n we would get this huge number back without the underscores it's only there for the developer so they you can understand the number much better but obviously some of you might be on team scientific notation so you might opt in to do this instead although that returns a float so we'd have to do that anyway let's go back a step and remove these underscores so instead of printing n the way it is we're going to turn this into a F string and use use the curly braces as always then we're going to insert our number and add an underscore after the colon and this will give us the same result as from earlier it's going to work as a thousands separator and that can make it much easier to read big numbers and you also have the option to do this using the comma so it's going to give us the exact same result except with commas being used as the Thousand separator and unfortunately these are the only two separators we can use in Python if you want to use a different separator you're going to have to Define your own custom functionality party trick number two imagine you have a variable and I'm going to be quite creative here and call this variable which will be of type string and that you want to align this text in a certain way when you print it well to achieve this we're going to use an F string once again insert our variable and then use the right arrow to write align the text and in this example example I'm going to make this variable consume 20 characters of space So at the moment this consumes three characters of space which means if we have 20 characters of a space this is going to insert 17 spaces before the variable so when we run this we're going to have the variable pushed all the way to the right of these 20 Spaces by default we have the left alignment so there's not much purpose in doing this other than giving that some space because with that you can also add let's say a colon after and it will always be maintained in the same spot or actually now that I think about it since it's the default you don't even have to add that left Arrow you can remove it completely and you will still get 20 spaces otherwise you can also Center align this by using the up Arrow now if we run that you'll see that the variable will be placed perfectly in the center just as we told it to do so right here but the fun doesn't stop there we can also specify a fill element so maybe for the first one we want to fill the empty spaces with an underscore and the second one will take the hash symbol and the bottom one will take a pipeline now when we run this again we're going to get this crazy output instead of just having empty spaces we're going to have the character that we selected filling that empty space trick number three for this example I'm going to import date time from date time and I'm going to get the current date and time so now of type date time is going to equal datetime do now and Python's pointing out that I'm higher something because I imported from Daye time from Daye time but what I really wanted to do was from Daye time import date time anyway once again we're going to use the magic fstring syntax and pass in that at date and time now with date time we can also provide date format specifiers such as percent day percent month and percent year and it will format our date time into this format as you can see in the console it's the 9th of February in the year of 24 we can also use other ones such as 24hour format followed by the minute and the second and it will format it the way we describe it we can also get the local version of our date and time by using percent C and it will end up giving us our local version of the date and time otherwise if you like the AM PM format you can do something like this you can use percent uppercase i and percent p and that will give us first the 12h hour format followed by pm and am right now here it's 10: a.m. and if you want to learn more about this syntax just do a quick Google search on date time format specifies and you'll see all the crazy syntax that allows you to play around with your date and time this video was sponsored by me on top of creating tutorials for YouTube I also create professional courses for learning python this month there's a valentine sale so in case you're looking to master the Python language learn data analysis or build some real life projects make your way to indent iio select the project and tap on start learning the discount is automatically going to be applied at checkout and on top of that you have 30 days to decide whether the course is right for you or not anyway let's get back to the video super crazy tip number four sometimes you're going to end up with some crazy num such as 1 234. 5678 and if you print this number it's going to end up like this in the console in many cases you might not care about all the these decimals and sometimes you even get much much longer decimals that you will surely want to shorten because it's insignificant information that you don't really care about in most cases so of course one thing you can do is round it to the amount of decimal places you want and that will work perfectly fine but we also have the option to achieve the same thing with f strings so here we'll pass in the number followed by 2f which tells python that we should format this to two decimal places or we should round it to two decimal places so it will give us the same result except in an FST string which means we can also do things such as add some extra text and the result will equal this result over here now we can also duplicate this and change this to 0f and what that's going to do is completely remove the decimal from our result and there's one thing that's really cool that you can do in combination with this syntax Tex and to show you I'm going to duplicate that change this to 3F and here I'm going to insert a comma in front of the dots and that's going to use the Thousand separator in combination with the rounding so you can use those in combination if you really want to take formatting those numbers to the next level and finally for trick number five which is probably my favorite fstring formatting trick I'm going to show you a very quick way to debug your code using FST strings so here we can have a which is an integer of five and B which is an integer of 10 then we're also going to have a variable of type string which will equal Bob says high and to start off we're going to try to find out what the sum of a plus b is while showing the user that we are adding a to B so a plus b equals a + b right now if we were to run this we would get this as an output which is quite descriptive it tells us exactly the operation that we performed to get that result but this took a lot of effort and energy and as a professional python developer I don't want to write six extra characters I'm just joking of course so let's look at that incredibly convenient sugar syntax that gives us the exact same result and this is actually quite cool all you have to do is remove this section here and inside the curly brackets add an equal sign this space between the equals will be maintained which means if we were to run this we're going to get a plus b equals 15 so anything in front of the equals will maintain its dignity by staying exactly the way you see it and in general you'll see people using it like this without any spaces personally I absolutely hate that approach because it's quite combined and makes it quite illegible so what I like to do is add some spaces around it but your code editor is going to complain so you need to pick your poison but really the cool thing about this is that no matter what operation you perform such as trying to convert a to a Boolean it's going to maintain the representation of that operation and give us the output and the same thing will go for my variable if we were to print this it's going to say that my VA is equal to this string otherwise you need to go through the pan of actually manually typing that so my VAR equals my VA and that's cool all but maybe someday you're going to change this variable name and that means that of course you need to change all the occurrences of where you hardcoded it anyway those were my five favorite FST string formatting tricks FST strings are a lot of fun and I play around with them all the time so I definitely recommend you memorize a few of these because they can really help increase the efficiency of your workflow but yeah that's actually all I wanted to cover in today's video so as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 276,959
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: EoNOWVYKyo0
Channel Id: undefined
Length: 10min 2sec (602 seconds)
Published: Mon Feb 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.