“print” treats the % as a special character you need to add, so it can know, that when you type “f”, the number (result) that will be printed will be a floating point type, and the “.2” tells your “print” to print only the first 2 digits after the point.
If you have learned C, C++, or Bash, you would be familiar with the 1.333333 1.333333 1.3333337 function. It is used for printing formatted data to stdout.
Question - Does Python have something similar to 1.333333 1.333333 1.3333338?
Answer - Not really. Python only provides the 1.333333 1.333333 1.3333339 function.
However, you can mimic the behavior of # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")0 by using string formatting.
This is where terms like width and precision come into play.
In this article, you will learn the intricacies of using width and precision with string formatting by comparing 3 different ways of string formatting.
What is 4 divided by 3?
Let’s print the floating-point representation of # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")1.
Code
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")Output
1.333333 1.333333 1.333333Each of the above formatting methods, produces the same output. The output of # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")1 has 6 digits after the decimal.
Let’s try to limit the number of decimal places to 3.
Code
# Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")Output
By using # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")3 as format placeholders, we are able to contain the number of decimal places to 3.
Understanding the structure of 0.3f
Let’s generalize # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")3 into a formula - # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")5
- # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")6 represents the minimum width or padding of the output string.
- # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")7 represents the maximum number of characters after the decimal.
- # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")8 symbolizes floating-point representation.
Let’s alter the # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")6 and # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")7 values and see how the output changes.
Code
# Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")Output
1.33 1.33 1.33You can see that your result contains empty white spaces to the left of each output.
So what happened? Let’s dive in.
Understanding width and precision in Python
Stage 1 - Using only 1.333 1.333 1.3331 representation
If you just use 1.333 1.333 1.3331 representation, the output will have a width/length of 8.
Code
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")Output
1.333333 1.333333 1.333333Stage 2 - Adding width
Let’s add only a 1.333 1.333 1.3333 placeholder to 1.333 1.333 1.3331.
Code
# Using % operator print("%10f"%(4/3)) # Using format method print("{:10f}".format(4/3)) # Using f-strings print(f"{4/3:10f}")When the user-defined width is different(greater or smaller) than the original width, the length of the output is changed to the user-defined width. In the above example, the new length of output will be 10 characters long.
Output
The resulting output is now right-aligned with extra 2 white space characters added as padding to the left.
However, if the user-defined width is 0, the original length of the output is maintained.
Try providing the 1.333 1.333 1.3333 as 0. 1.333 1.333 1.3336 will provide the same result as using # Using % operator print("%0.3f"%(4/3)) # Using format method print("{:0.3f}".format(4/3)) # Using f-strings print(f"{4/3:0.3f}")8 floating-point representation.
Code
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")1Output
1.333333 1.333333 1.333333Stage 3 - Adding precision
Let’s now change the number of decimal places in the output to 2. This can be done by providing the 1.333 1.333 1.3338 value.
Code
# Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")Upon fixing the 1.333 1.333 1.3338 to 2, the resulting output will only have 2 characters after the decimal. All the other characters after the 2nd decimal places are chopped off.
Hence, the output string moves even further to the right, creating more empty white spaces.
Output
1.33 1.33 1.33Using width and precision with integers
Formatting integers with the Width
Formatting integers with width has a similar effect as formatting floating points.
Code
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")5Output
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")6Formatting integers with precision
However, providing precision with # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")0 formatting has no effect and with # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")1 and # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")2 method, it throws an error.
Code/Output - precision with % operator
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")7Code/Output - precision with format and f-strings
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")8Using width and precision with strings
Formatting string with Width
Unfortunately, the default alignment differs between # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")0 formatting (old style) and formatting with # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")2 method and # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")1 (new style).
The old-style defaults to right-aligned while for new style it’s left.
Code
# Using % operator print("%f"%(4/3)) # Using format method print("{:f}".format(4/3)) # Using f-strings print(f"{4/3:f}")9Output
1.333333 1.333333 1.3333330You can change this behavior easily by using certain operators.
How to align your strings left and right?
To right-align your string, use # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")6 operator with the new formatting methods.
Code - To right align strings
1.333333 1.333333 1.3333331Output
1.333333 1.333333 1.3333332To left-align your string, use # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")7 operator with the old formatting method.
Code - To left-align strings
1.333333 1.333333 1.3333333Output
1.333333 1.333333 1.3333334Formatting string with precision
When you add 1.333 1.333 1.3338 to string formatting, the characters of the string are truncated.
Let’s say, you provide a precision of # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")9. All the characters except the first # Using % operator print("%10.2f"%(4/3)) # Using format method print("{:10.2f}".format(4/3)) # Using f-strings print(f"{4/3:10.2f}")9 characters are truncated.