Controlling Symbol Displays
00:00 Let’s carry on exploring how you can customize a fixed-width string when you’re dealing with floats. We’ve seen that if you set a width larger than the number of characters needed to display your formatted number, the string is padded with spaces.
00:17 However, you can choose another character instead of space. Spaces are the default. You could, for example, use the star symbol or the asterisk, and you can include it right after the colon, before the caret symbol, which is aligning center in this case.
00:33 And you can see that the three spaces that have been filled, one in the beginning and two at the end, are no longer spaces now they’re asterisks. We can also see this with the align left version or indeed the align right version.
00:49 In both instances, the extra characters have been filled not with spaces, but with asterisks. And if you prefer a different symbol, hyphens, for example, or almost anything else you want.
01:02
There we go. You can put an a
in there and you’re filling in the gaps with the characters you choose.
01:09
And let’s get back to the value with the asterisk because the value with an a
doesn’t really make much sense.
01:16 As you can imagine, if we print the negative of the value, a minus sign appears in front of a number and therefore now there are only two extra characters left.
01:27 And that’s why there are only two asterisks at the end. But what if when you have a positive value,
01:33 you also want the plus sign to appear. And as you can guess, the answer is you can. There’s another symbol we can use.
01:43 The plus sign in front of the 12, which is the integer that represents the width of the entire string, indicates that you want the f-string to always put in the plus sign.
01:54 Normally, for positive values, the plus is not shown, but if you want to show it, and perhaps in this case it’s even better to remove the asterisk and the alignment and simply put the plus there.
02:04 And now whatever the value, whether it’s positive or negative, you’ll always have the positive sign if it’s a positive value. And of course, if the value you’re plotting is a minus value, then the plus is no longer there.
02:18 It’s replaced by the negative sign.
02:22 Let’s finish this lesson by summarizing the key points from this lesson and the previous one when we discussed the customization of the widths of formatted strings. So you can set the overall width of the string using an integer after the colon, for example, in this case there’s a 12 after the colon, which means that the overall width of the string is 12 characters long.
02:45 This number represents the overall width, which may include symbols like the dot, comma, or any other symbols, including the number.
02:55 The f-string formatting mini-language allows you to combine this width parameter with the other format specifiers that you learned about in previous lessons.
03:05
This example has after the thousand, which is the number we want to display, there’s the colon and then the format specifier is 12
,.2f
.
03:15
The 12 represents the overall width of the string. The comma shows that you want to put in the thousand separator, and the .2f
indicates that you would like to have two digits after the decimal point.
03:29 You can also align the output to the left or to the right or align center using the less than, greater than, or the caret symbol. In this example, there’s a less than symbol following the colon.
03:42 This is a bit like an arrow pointing left therefore, the number will be aligned to the left.
03:48 And the space is used to pad whenever the width of the string is longer than the number of characters you have. But you can override this default space character by adding any character you want at the start of the format specifier.
04:03 You can only use this when you are using one of the alignment modifiers such as less than, greater than, or the caret symbol. In this example, there’s an asterisk or a star right after the colon, which means that symbol will be used to pad the string instead of spaces.
04:21 And finally, if you want your positive numbers to show a positive sign at the beginning, a plus sign, then you can use a plus modifier just before specifying the width.
04:31 And this will add a plus symbol to your positive values.
04:36 And these aren’t the only format specifiers you can use. You’ll learn about more later in the course but I’ll also remind you that you can read the accompanying tutorial which offers more detail about all of these topics.
Become a Member to join the conversation.