We have covered about the
format() function in Python in a separate post where we did mention various use cases for which this amazing function can be used in two part series:
format() function in Python - Part 1
format() function in Python - Part 2
If you want to take a deep dive and learn all the possible use cases of the format() function, we would suggest you to go an read both the previous posts.
In this post we will only cover one use case, which is how we can use the format() function in Python to format numbers using various format specifiers. As you have already seen in the title, the different format specifiers are:
fill - Used to fill any character to a given number, either in the beginning or at the end.
align - The alignment could be <, >, ^ or =. Where = is for default behaviour, < is to align the number to the left, > is to align the number to the right and ^ is to align the number to the center. You must be wondering what does alignment has to do with a number, well, wait a little, you will all this in action in the code example below.
sign - This could be a +, - or nothing. This is used when you wish to explicitly specify whether the number is a positive number or a negative number.
width - An integer value specifying how wide the value has to be formatted to. If the given number is not equal to this width, the fill value specified comes into play to fill in the empty spaces.
precision - This value tells how precisely the value passed to the
format method has to be formatted. This comes into picture if the type specified is floating point number.
type - This specifies the type - binary, octal, exponential, integer, float etc.
Time for some Examples
We will be showcasing multiple examples, starting form using single format specifiers to combinations of format specifiers.
Let's start with specifying the width value with no other format specifier.
In the example above, we have specified 8 as the width value while the number has only 4 digits, and hence the remaining spaces are filled with empty spaces. Now, we can use the fill fomat specifier to fill the empty spaces with some special character that we want.
ValueError: Invalid format specifier
We will get an error, because, if we want to specify the fill format specifier, we must specify it with align format specifier, to mention where to put the fill parameters.
Try using some other align format specifier value, like ^, which for aligning the number in center.
Also, you can try playing around with the value of the width format specifier, which we have set as 8.
Now, let's use the sign format specifier as well with the format method, to add a sign to our number.
In the output, we get an additional positive sign symbol in front of our number.
NOTE: One important point to note here is that the order of the format specifier matters. If you will provide the format specifier in any order, you will get error. The order should be: fill, align, sign, width, precision and then type.
Let's add the type format specifier,
We will get the same output, because
d format specifier is for integer value and our value is already an integer value. Try with
b format specifier, which is for binary number representation, and you will get the binary representation of the number given.
Here, as the binary representation of the given number has more digits than the minimum specified value which is 8, hence the fill format specifier is not added because the width of the number is already more than the minimum width provided using the width format specifier.
Let's use the precision format specifier now with f as the type format specifier for floating point value.
This will return a floating point representation of the given number with a precision of 3 digits after the decimal point.
We have compiled all the above example in the below terminal, where you can run and see the output. Also, you can try different values and see the output.
In this post we learned, how we can format numerical value into different formats using the
format() method in Python and by using various format specifiers available.
You may also like: