Python 3, an Illustrated Tour Transcripts
Chapter: Strings
Lecture: String Formatting

Login or purchase this course to watch this video and the rest of the course contents.
0:01 This video will discuss string formatting and pep 3101. Pep 3101 introduced the format method on a string,
0:10 note that this exists in later versions of Python 2. Prior to that, there was a c-like way of formatting strings
0:19 so we would use percent and then some character after that typically s means I'm going to represent this as a string.
0:28 So here I have two variables and then we use the mod operator and pass in a tuple of things that we want to stick into these placeholders here.
0:38 So %s %s will take hello and stick it into the first place holder and in the second place holder world will pop in.
0:46 Pep 3101 we make this a method on a string format is a method on a string, and we're using curly braces
0:53 to specify our placeholders here, in this case the 0 and 1, 0 refers to who's in the zero position
1:00 and the 1 here refers to who's in the second position. Again, Python is a zero based language, so 0, 1.
1:08 One of the nice things about the 0 and 1 which are completely optional is if I want to say Hello, Hello, Hello, I could say {0} {0} {0}
1:17 I wouldn't have to put in Hello three times in as arguments to format. Using the format method, there's a mini language
1:25 and it allows us to do some things, we can insert some Python expressions. So in this example, I'm passing in a dictionary into the format,
1:36 and note that I'm doing an index operation here with square brackets here, so I'm saying on what's ever passed in I want you to do an index operation
1:45 and pull age off and we get 50 there. We can do a similar thing with attribute access, Here I say .age give me the age attribute of my p class
1:56 and I'm passing in p there and he prints out 50. So we can do some simple expressions there index and attribute access.
2:05 You try and call a function or do something like that and it's going to bark at you and say that you can't do that.
2:11 Here's an example of trying to call upper. So we pass in a string and we want to say
2:16 hey give me the upper attribute, but also invoke it with parentheses here and it gives me there's no attribute upper with parentheses following it.
2:25 So it's trying to do some things to not allow you to invoke or call arguments there.
2:32 We talked about position and here's just an example of using position if I want to say na na na na hey Jude,
2:39 I can repeat na 3 times just by putting the position in there, note that I don't have to repeat na multiple times.
2:47 Now, there's a whole little language for string formatting here. This is basically what can go where,
2:53 this next slide here is the crib sheet that tells you what can actually go in the where so I'll go over these briefly, don't try and memorize all this
3:05 you can refer back to this if you need to but a lot of the times you don't need all these different formatting options.
3:14 So a fill character, you can specify a fill character the default character here is a space and you don't need to put anything in there.
3:24 There's an alignment that allows you to center right or pad align things by using one of these four characters here,
3:33 less than, greater than, equal or the caret. There's a sign, we can stick in a sign here so we can put a plus a minus or a space
3:41 if we have a plus in there, then we're going to put a sign in front of all numbers. If we have a minus in front of there,
3:50 then we're going to put a sign in front of negative numbers and with a space we will put a space in front of positive numbers
3:58 and the sign in front of negative numbers. We can put this hash in there and that just says if I've got a number that's a binary, octal or hex,
4:09 I want you to stick 0b, 0o or 0x in front of those respectively. There's an option here to do zero padding so we can stick in a zero there
4:23 and if we have numbers we'll get padding after that, the default there is space so it doesn't stick in padding,
4:29 but if you want to have zero padding on the left you can do that. We can specify the minimum width if we want
4:36 something to take at least 3 spaces, we can say 3 in there. We can also specify a thousands separator, there is no thousands separator by default,
4:45 but if we want to have a comma as a thousands separator we can put that in there. Also, we can put a precision following a period,
4:53 this is for floating-point numbers. If you want to have five digits of precision, you can put .5 and that will give you the precision.
5:02 If you have a string that's going in, then this will give you the max length of the string.
5:07 So if I want to take uh up to 5 characters of that you can put 5 in there. And finally, at the end here, we have a type.
5:13 There are various types that we can specify, these are all on the bottom here.
5:18 The default is s which means just give me the string representation of that. We can also provide r to give us the repr.
5:25 There are various options that we can use for numbers that are integer numbers and here are some floating-point options we can use as well.
5:34 So e for lowercase exponent, E for uppercase exponent f for fixed point, g general, it changes between fixed point and exponent
5:45 to try and be nice to you. And n is a locale specific general version if you're in a different locale
5:52 and a % sign will convert a floating point number to a percent. So if you have .5 it will convert that to 50,
6:00 so lots of options and things that you can do in there. Don't memorize this, but you can come back and refer to this if you need to.
6:07 Here are some examples of formatting a string. Here I say that I want to format Ringo in 12 characters and surrounded by asterisks here.
6:18 So we put a colon here, anything following the colon is the formatting options. You can see that we have an asterisk, that's the fill character
6:27 and then we have a caret and then we have 12 so we're going to take 12 characters and center that, caret means center.
6:33 Here's one here, formatted percentage using a width of 10 with 1 decimal place and a sign before the width padding.
6:39 And so we see there's a colon, after the colon is going to come our formatting options
6:44 we're going to use an equal that says put the space after our sign there and we're going to use 10 characters and one character of decimal precision.
6:56 And then, since it's a percent, we're going to multiply it by 100 to convert it to a percent. And so we see 44 divided by 100 would be .44
7:05 but this is going to multiply that by 100. Here's a simple binary and hex conversion. We just put :B and 12 as binary is 1100 12 as hex is c.
7:19 There's a little link at the bottom here pyformat.info, it's a nice website, you should go visit that
7:24 and it has a bunch of examples of doing string formatting in what it calls the old school way of doing it,
7:31 which is using the mod operator and the c-style placeholders and some examples of doing that with the newer format method,
7:39 great examples in there, nice little resource to know about when you forget the formatting options and want to see some examples.


Talk Python's Mastodon Michael Kennedy's Mastodon