Python Tutorial: String Formatting - Advanced Operations for Dicts, Lists, Numbers, and Dates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody how's it going in this video we're going to be looking at string formatting operations in Python now a lot of people know the basics of string formatting but there are a lot of options beyond the basics that are available that will allow us to format our strings in exactly the way that we want so I want to run through a few examples here really fast just to show some of the different things that we can do with formatting our strings so first of all if you're not already using some kind of formatting when you print out your strings then you definitely should be so sometimes I'll see people using string concatenation to display information and that's exactly what I'm doing on this first line here and there are a few things wrong with this so you can see first of all it's not very readable you have to open and close strings in different places plus you have plus signs everywhere whenever you have an integer you have to cast those two strings and also you have to remember to put spaces in the correct location so this middle string here I have to remember to put in a space at the beginning and at the end and if I mess that up then it can bunch my string together when I print it out so you can see if I run this code that it does work but there are much better ways to do this so it's much easier to use the formatting option and that's what we're going to take a look at here so let me uncomment out this so you can see that this is a lot easier to read now we have these braces here as placeholders and after our string closes we run this format method and then we pass in the values that we want to replace our placeholders with now if we do it the way that I did here and don't add anything to these placeholders then what it's going to do is it's just going to pass our first value here to our first placeholder and our second value to our second placeholder now if you want to you can explicitly number your placeholders so in this example here it's the exact same example but now I've numbered my placeholders so now what this is saying is that we want this 0 here is the first value that you pass into format and then this one is the second value that you pass in the format and if I save that and run it you can see that it still works now this is more you well when you have placeholders for values that need to be repeated so in this next example that I have here you can see that I have this tag variable and this text variable so in my string I'm putting the tag and some opening brackets here and then I'm putting in a placeholder for the text and then I'm putting the tag inside these closing braces here and then the values that I pass in the format tag will go where all of the zeros are for our placeholders and text will go where the one is for our placeholder so if I save that and run it you can see that it prints out the value that we expected okay so now we can also grab specific fields from our placeholders so in our previous example we were passing in a dictionary to our format and within our format we were accessing the name and the age of this dictionary from directly within the format here but we can actually access these fields from directly within the placeholders so within the placeholder here for zero I'm just going to put these brackets here and do name and for one I'm going to do age and then I can take these off of our dictionary here and just pass in the dictionary so let me save that and run it and you can see that that still works now another thing that you might notice is that now you can see that I'm just passing in this dictionary in the format twice I have person and person so really what I can do is I can just make both of these a zero index to take the first value from format and then I can get rid of the second value and now what this is going to do is it's going to pass the person dictionary to all of our placeholders and here it's going to access the name and here it's going to access the age so if I save that and run it then you can see that it still works now this is also how you would access values of a list too so for example let me do L equals and make a list here and I'm just going to do the exact same value so I'm going to do Gen and 23 and here I'm going to pass in that list and now instead of name I'm going to grab the first index there and then the next index and save that and run it and you can see that that works also okay so that is how you access values from dictionaries and lists but you can also access attributes in a similar way okay so I have a small test class here called person and this has a name attribute and an age attribute and then here I'm making an instance of this class person with the name Jack and the age 33 now if I want to print this out it's almost the same as what we did with the dictionary but now instead of using the brackets we're just going to use this dot attribute to grab that value so you can see here I'm still just passing in this single object into format and it's going to come in here and grab the name attribute and the age attribute so if I save that and run it you can see that that worked okay so we can also pass in keyword arguments to format so for my example that I have here I have my placeholders and I'm just passing in some keywords into the placeholders now within format here instead of passing in a specific object I'm setting these keyword values so I'm setting name equal to Jin and the age equal to 23 so now anywhere that it sees a placeholder that matches that keyword then it will fill it in with that value so if I save that and run it then you can see that that worked right now this is the method that I usually use to print out dictionaries because I think that it is a little bit more readable now if you know about unpacking lists and dictionaries then you'll probably realize here that we can just unpack our dictionary from before into format and it will find all those keywords for us to use so I actually accidentally deleted that dictionary that we had from before so let me go ahead and make that again so let's see that was named and I'll just do Jin and I'll do the age as 23 and let me fix that curly brace there okay so now in this example here I'm using these keyword arguments and if I just unpack that dictionary then it will fill in those keyword arguments for us so let me save that and print it and you can see that that worked so to me that's the most readable and most convenient way to print out dictionary values okay so now let's take a look at how we can format and print out numbers so in this example that I have here all I'm doing here is looping through and printing out the numbers 1 through 10 so now what if I wanted all of these numbers to have 2 digits and 0 pad my single digit values with a 0 now in order to do this I'm going to have to add formatting to our placeholders and we can do that by adding a colon here so now we can add whatever formatting that we'd like so I went to 0 pi add my digits to 2 so we can do that just by doing a 0 to here and if I save that and run it now you can see instead of 1 2 3 it's 0 1 0 2 and then when I get down here to 10 it doesn't pad it because it's already two digits now if I was to make this a 3 and save that and run it now you can see that it 0 pads all the way up to 3 digits ok so now let's look at using format to do decimal places so here I have PI written out to 8 decimal places so let's say that I want to print that out but I only want to print out to 2 decimal places so let me add my colon so that it knows that we want to do some formatting and now I can specify that I want two decimal places just by doing a point 2f so now if I save that and run it you can see that it says pi is equal to 3 point 1 4 and again if I change that to 2 a 3 and save it and run it and it does up to 3 decimal places ok so let's look at an example for say that we wanted to print out a large number and we wanted some comma separators so that it was more read more easily readable so we can easily do that just by adding a comma after our colon so I'll do a : here to specify that we want formatting and I'll just put in a comma and if I save that and run it you can see that we have our comma separators here on these large values and you can chain this formatting together too so let's say that we wanted the comma separated values and we wanted to display up to two decimal places so I write after the comma I could do my point 2f that we did from before and if I run that you can see that we have our comma separated values and it added two decimal places on to the end okay so let's take a look at an example for how we can format and print out dates so I think this is extremely useful if you need to print out date time information it allows us to display the information in just about any way that we want so it's especially useful if you're printing out dates for logs or creating reports or anything like that so I just created a date here for September 24th of 2016 so if I just print out that date variable then you can see that it's not too bad it's pretty easy to tell what it's doing it's printing out the year the month the day and then the hours minutes and seconds but let's say that we wanted it in this format here we wanted the month the day and then a comma and the year so let's take out this print statement here and let's try to do that okay so first of all we're going to want to add our colon here to specify that we want to do some formatting to this and now I'm going to go to the website and get the values that we want to use here so we want the full month and we can see here that the full month is % B and we also want the day of the month and we can see that the day of the month here is % d and we also want the year here and there's a couple of options for the year but we're going to go ahead and just do the four digit one here with the capital y so like I said it's completely fine if you don't know these formatting options just whenever you're trying to do something you can look it up in the documentation and know how to get it done so let's go ahead and pass these in so the name of the month was a % B and then I'm going to do the day as % D and then I want the comma and then the % a capital y for the year so let me go ahead and print that out okay so that worked the way that we wanted it to so you can see how formatting your strings like this could be extremely useful for printing out dates so now let's do a slightly more complicated example and let's try to do it in this format here let's say that we want the name of the month the day the comma the year then we want to say that it fell on a and then put in the day of the week and that it was the day of the year so I want to put the day of the year here so we want to format our string to where it looks like this so let me uncomment out what we have here so far now we already know how to do this first part here so that was just a comm : and we want our % b % d for the day comma % capital y so now let's also go back to the documentation and find the day of the week and the day of the year so i'm going to go and grab that so we can see here that the day of the week is over here at capital a and the day of the year is this % j so if i go back here and do our colon and a % with a capital A and then over here I'm going to do the colon with % lowercase J now if I just try to run this as is then you can see that we get an error now the reason that we got an error is because we have three placeholders but we're only passing in one value to our format so if you remember I can just do the index here so I can say that we want this to be 0 index and the first value that we pass into format so now even though we have three placeholders it will replace all those placeholders with our single value that we're passing in to format so now if I run that you can see that it gives us the output that we wanted so now you can see here that it says September 24th sixteen fell on a Saturday and was the 268th day of the year so I think that's going to do it for this video I hope it helped in knowing what all is available when it comes to string formatting and also maybe gave you some ideas for how you can use this in your own applications but if you do have any questions just feel free to ask me in the comment section below be sure to subscribe for future videos and thank you all for watching
Info
Channel: Corey Schafer
Views: 227,818
Rating: 4.9555082 out of 5
Keywords: Python, Python Tutorial, String Formatting, Format, Python Strings, Python print, Python Format, format, print, Interpolation, String Interpolation, Python String Interpolation, Learn Python, Logging, Python Logging, Python Placeholders, Programming Tutorials, Software Engineering
Id: vTX3IwquFkc
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Thu Mar 10 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.