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