Customizing Width and Alignment
00:00 In this lesson, you learn how to customize the width of a string when using Python’s f-strings. There are formats that require strings to have a fixed width and you may come across these as you program, but you may also want to display things in a clean way.
00:15
And for that, you may want to ensure that several strings all have the same width. You’ve already seen an example of this in the first lesson. You may remember we had the dictionary products
that had three items: “Coffee”, “Cookies”, “Apple”, and they all have a price.
00:31
Let’s create the price list for this. We can use a for
loop: for item, price in
products.items()
. You don’t want to loop directly through the dictionary.
00:43
You want to use the .items()
method, which pairs the key and the value together as a tuple pair. And therefore, item
will be the name of the item and price
will be the price.
00:54
And now you can print()
using an f-string. And let’s keep it simple. Let’s print()
the name of the item and now put a space, let’s assume these are dollars and the price.
01:06 And let’s see what happens when we print this. We do have the price list, but as we’ve seen in the first lesson, it’s not in the format we would expect it to be.
01:15
The prices are not aligned with each other and “Cookies” is only showing us $3 without the .00
, which even though we can assume that that means $3 exactly, normally when we display prices, we want to show them all in the same way.
01:29
So let’s rewrite the for
loop. Instead of using the same things with it before, let’s put in some format specifiers and let’s focus on the name of the item.
01:40 I can put a colon, but this time all I’m going to do is put in an integer, 10 in this case.
01:47
And you can see that the output has changed. The 10 after item
means that you want to use 10 characters to display that particular string. Since “Coffee”, “Cookies” and “Apple” are all words with fewer than 10 characters, the rest is padded with spaces.
02:06 And this means that “Coffee”, “Cookies” and “Apple”, even though they’re names of different lengths, they all take up exactly the same space and display.
02:14 And that’s exactly 10 characters.
02:18
And we can run the for
loop again using the knowledge you have from the previous lessons. In fact, this is a good time to pause the video and try it out yourself.
02:28
How can we display this price in the way we expected with the .00
for “Cookies”?
02:34
And the answer is we can put a colon .2f
02:39 and there we have our display. Now let’s explore the fixed width in more detail. And especially let’s do it with floats.
02:50
You can start a new REPL. And let’s define value
, which is going to be the float 12345.6789
. As you can see, this is not a round number, but it’s fine.
03:01
It’s a long number with a dot somewhere in it. Let’s print()
this value using f-strings and we’re going to print()
this using a number of different formats.
03:10 Let’s start with what we already know. So let’s put an f-string.
03:15 I’m going to also use two pipe symbols to show the beginning and the end of the string. And you’ll see why as we build this. It’s a way of showing where the strings begin and end so we know how wide they are.
03:27
So as I said, let’s start with something we know: value
the colon to show. We want to put the format specifier .2f
, which means use two digits after a decimal point.
03:38
And there you can see the two pipe symbols showing us the width of the string, which contains 12345.68
. It doesn’t have .6789
because we only wanted two digits after the decimal point.
03:52 What happens if I add an integer before the dot? For example, 12. We know what the integer after the dot refers to. It means this is how many digits we want after the decimal point.
04:05 The integer before the dot tells us the overall width of the string. It’s not how many values we want before the decimal point. It’s the full width of the entire string.
04:17
So here you can see that 12345.68
is eight characters. I’m including the dot and the numbers after the decimal point. Since you want 12 characters, there are four spaces added before the number.
04:33
And you can see that there are some spaces and there are exactly four spaces from the beginning of the string, which is shown by the first pipe symbol. And then there are those spaces before the 12345.68
starts.
04:46
In fact, we could fit the entire string using eight characters. Because that’s how long 12345.68
is.
04:58 So what happens if I put a smaller number in there, for example, two?
05:02
How can the number 12345
with two digits after the decimal point fit in two characters? The answer is it can’t. So Python’s f-string will override it and say, well, the minimum I need is eight characters in this case.
05:17 So any number lower than that will be expanded to eight characters.
05:22 So let’s go back to the version that had the width of 12 characters. So in between the pipes, we have now 12 characters.
05:30
What if we want the thousand separator? Where should this go? And the answer is that it goes after the width, but before the dot. So as you can see, 12.2
is not the number 12.2
.
05:44
They’re simply parts of the format specifier that all represent a different aspect of our formatting. The first integer is the width. The comma means we want a thousand separator and then .2
shows how many digits we want after a decimal point.
06:01 And now the overall number of characters is nine because we have the comma. But since our string is 12 characters wide, we still have three spaces to spare.
06:14 And you may have noticed that the number is aligned to the right. This is the default when we have numeric formats. It’s different if you had a string. If you tried this with a string, you’ll see the string is aligned to the left, but numbers are aligned to the right.
06:28 But what if you don’t want that? What if you still want 12 characters, but you want the number to be aligned to the left? You can. And as you can guess, there’s another symbol we can add to our format specifier.
06:40 And this is the less than symbol, but you can think of it as an arrow pointing to the left. This means we want to align left. And now you can see that we still have the same number formatted in the same way, but it’s aligned left within the 12 characters.
06:56 You can probably guess what align right is. You don’t need it when you’re using numbers because the default is align right. But in some other instances, for example, if you’re using strings with a fixed width and you want to align right, then the greater than symbol or if you like, an arrow pointing to the right.
07:13 And finally, how about align center? Well, and if an arrow pointing left and an arrow pointing right are used for align left and right, that’s the less than or greater than symbol.
07:24 That’s the arrow pointing upwards, right? So this is giving us align center. And you can see that the number fits within the middle of the 12 characters, which is the overall width of your string.
Become a Member to join the conversation.