We often require displaying numbers with leading zeros. Storing and displaying numbers with leading zeros is a crucial aspect as it significantly helps in accurately converting a number from one number system to another. In this blog, we'll see various ways to display numbers with leading zeros in Python.
str.zfill()
function
The str.zfill(width)
function is used to return the numeric string with zeros automatically filled at the left side of the number of the given width
. If the value specified of this width
parameter is less than the length of the string, the filling process does not occur.
print(str(5).zfill(2))
print(str(50).zfill(2))
The above code provides the following output.
05
50
using rjust()
function
The rjust()
function adds padding to the left side of a string. Padding, in simple terms, is the injection of non-informative characters to the left or the right side of a given string, which adds no additional value to the initial string. We can use string padding to display leading zeros in a Python program.
The rjust()
function contains two parameters: width
and fillchar
. Out of these two, the width
parameter is mandatory and is utilized to specify the length of the given string after the padding process is completed. On the other hand, the fillchar
parameter is optional and represents the character that we want to pad the string with.
print(str(5).rjust(2, '0'))
print(str(50).rjust(2, '0'))
The code above provides the following output.
05
50
str.format()
function
The str.format()
utilizes the curly {}
brackets to mark the place where the variables need to be substituted in the print
statement.
It was introduced in Python 2.6 and can be used in all Python versions released after that up to Python 3.5. This function is incredibly popular among coders, and the programmers recommend this way to implement string formatting due to its very efficient handling in formatting complex strings.
The following code uses string formatting with the str.format()
function to display a number with leading zeros in Python.
print("{:02d}".format(5))
print("{:02d}".format(50))
The above code provides the following output.
05
50
String formatting using % operator
Yet another way to implement string formatting is using the modulus %
sign, sometimes even referred to as the string formatting or interpolation operator. Being the oldest of the ways you can implement string formatting, it works without any problems on almost all the versions of Python that are currently available for use on the internet.
The %
sign and a letter representing the conversion type are marked as a placeholder for the variable.
The following code uses the string formatting with the modulus (%)
operator to display a number with leading zeros in Python.
print("%02d" % (5,))
print("%02d" % (50,))
The code above provides the following output.
05
50
In the code above, the number 02
written after the %
sign sets the text width to be displayed and adjusted for the leading zeros, and the d
variable denotes the conversion type.
Using f-strings
Being introduced with Python 3.6, it is relatively the newest method in Python to implement string formatting. Additionally, it can be used in the latest versions of Python.
It is more efficient for implementing string formatting than the other two previous methods, the %
operator and the str.format()
function, as this one is quicker and simpler to understand. It also helps in implementing string formatting in Python at a faster rate than the other two.
The following code uses string formatting with f-strings
to display a number with leading zeros in Python.
print(f"{5:02d}")
print(f"{50:02d}")
The code above provides the following output:
05
50
In the program above, the string width and data conversion type are specified after the colon sign within the placeholder.
When it comes to using string formatting to pad the string with leading zeros, the use of f-strings
is recommended by professionals more than its other two methods. It is more concise, less prone to errors, and certainly improves the readability of the program.
Top comments (0)