Format a Number Width in Python
Last Updated :
15 Feb, 2024
Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python.
Format a Number to a fixed Width in Python
In this article, we'll explore various methods and techniques in Python to format numbers to a fixed width.
Format an Integer to a fixed Width in Python
This code demonstrates how to use f-strings in Python to format integers to fixed widths, with options to pad them with leading zeros or spaces, depending on the desired output format.
Python3
my_num = 12
# formatting integer to a fixed width of 3
print(f"{my_num:03d}")
# formatting integer to a fixed width of 4
print(f"{my_num:04d}")
# formatting integer to a fixed width of 6 (with leading spaces)
print(f"{my_num:6d}")
Format a Floating point Number to Fixed Width
Example 1 : Using Python f-string
Format a floating point number to fixed width, in the final print statement, float_num is the name of the variable that stores our floating point number, 7.4f means we want our output number to be of width=7 , with 4 digits after the decimal point.
If you notice the second line of the output, you can see that the right most number is 5 instead of 4. Our number at the fourth decimal place was incremented by 1 because if the value at the position from where the numbers have to be dropped is greater than 5, then the right most number in the output will be incremented by 1, else it remains the same.
Python3
my_float = 4.163476
print(my_float)
# formatting a float to fixed width of 7 with 4 digits after the decimal
print(f"{my_float:7.4f}")
Example 2: Using Python Round Function
We can use the round operator to fix the number of digits we need after the decimal point. It takes the floating point number and the number of digits as parameters.
Python3
float_num = 123.26549
# formatting float_num to have 4 digits after the decimal
print(round(float_num,4))
Format a Number to a Fixed Width using leading zeros
In order to use the str.zfill() function, we need to first convert the number to string. The str.zfill() function fills '0's to the left of the string to make it of the required length.
Python3
my_num = 9
# converting number to string and then applying zfill function
num_to_str= str(my_num)
# format my_num to fixed width of 3
result=num_to_str.zfill(3)
print(result)
# format my_num to fixed width of 5
result = num_to_str.zfill(5)
print(result)
Similar Reads
How to Round Numbers in Python? Rounding a number simplifies it while keeping its value as close as possible to the original. Python provides various methods to round numbers, depending on how we want to handle the precision or rounding behavior. In this article, we'll cover the most commonly used techniques for rounding numbers i
4 min read
How to format numbers as currency strings in Python Formatting numbers as currency strings in Python is a common requirement especially in the applications involving financial data. Formatting numbers as currency strings in Python involves presenting numeric values in the standardized currency format typically including the currency symbol, thousands
3 min read
Number System in Python The arithmetic value that is used for representing the quantity and used in making calculations is defined as NUMBERS. The writing system for denoting numbers logically using digits or symbols is defined as a Number system. Number System is a system that defines numbers in different ways to represen
6 min read
Format Numbers in Django Templates Formatting numbers in Django templates is essential for creating a polished and user-friendly interface. Whether you use Djangoâs built-in filters for common formatting needs, create custom filters for specific requirements, or leverage third-party libraries for advanced options, Django provides the
2 min read
Print number with commas as 1000 separators in Python In this program, we need to print the output of a given integer in international place value format and put commas at the appropriate place, from the right. Let's see an example of how to print numbers with commas as thousands of separators in Python.ExamplesInput : 1000000Output : 1,000,000Input :
1 min read