How to Add Leading Zeros to a Number in Python?


While working with numbers in Python, it is often necessary to add leading zeros to a number. Adding leading zeros to a number is important when you are dealing with formatted data or when you need to ensure that a certain number of digits are present in a number, or in a more readable form. In this article, we will explore different methods that can be used to add leading zeros to a number in Python.

What are the methods to Add Leading Zeros to a number?

In programming, it’s often necessary to format numerical data in a specific way to meet certain requirements for example adding leading zeros to a number, which means placing zeros before the actual value of the number to make it a fixed width.

Python is a versatile programming language that offers multiple libraries and methods to add leading zeros to a number. Below are some of thepopular methods to perform the task.

Method 1: Using str.zfill() method

One method to add leading zeros to a number is to convert the number to a string first and then use the zfill() method to add the leading necessary zeros. The zfill() method takes a single argument, which is the width of the resulting string or the string that will come as output. If the original string is shorter than the specified width, then the zfill() method adds zeros to the beginning of the string until it reaches the specified width.

The syntax of zfill() is −

stri.zfill(desired_width)

Where stri is the original string.

Example

number = 50
desired_width = 5
#first convert thenumber to string then use zfil method
number_str = str(number).zfill(desired_width)
print("Number after leading zeros: ") 
print(number_str)

Output

Number after leading zeros: 
00050

In the above example, we start with the number 50 and convert it to a string using the str() function. We then use the zfill() function to add leading zeros to the string until it has a width of 5. The resulting string is “00050”.

Method 2: Using the rjust() method

The other method to add leading zeros to the number is to use the rjust() method which can be used to right justify a string and fill the remaining space with a specified character. To add leading zeros to a number, we can convert the number to a string and then call the rjust() method with the desired width and fill character.

Example

number = 50
desired_width = 5
number_str = str(number).rjust(desired_width, "0")
print("Number after leading zeros: ")
print(number_str)

Output

Number after leading zeros: 
00050

In the above example, we have first converted the number into a string and then we used the rjust() method which has two parameters one is for the width and the other is the value that we will use to fill to add leading zeros to the number 50.

Method 3: Using string formatting

Another method to add leading zeros to a number is to use string formatting. Python’s string formatting syntax allows you to specify the width and precision of a number, among other things. To add leading zeros to a number, we can use the “0” character as a fill character and specify the desired width.

Example

number = 50
desired_width = 5
number_str = "{:0>{}}".format(number, desired_width)
print("Number after leading zeros: ") 
print(number_str)

Output

Number after leading zeros: 
00050

In the above example, we use the string formatting syntax to format the number 50 as a string with a width of 5 which adds leading zeros. The “{:0>{}}” part of the string formatting expression specifies that we want to fill the string with zeros (“0”), that we want to right-align the number(“>”), and that we want to specify the width using the second argument to the format() method.

Method 4: Using f-string

Another method to add leading zeros to a string is to use the f-string syntax. From Python 3.6, a new string formatting syntax called f-string was introduced. The f-string provides a simpler and more intuitive way to format as compared to the older string formatting syntax. To add leading zeros to a number we can simply include the desired width and fill the character with zeros in the f-string expression.

Example

number = 50
desired_width = 5
print("Number after leading zeros: ")
number_str = f"{number:0>{desired_width}}"
print(number_str)

Output

Number after leading zeros: 
00050

In the above example, we use the f-string syntax to format the number 50 as a string with a width of 5 and leading zeros. The f-string expression "{num:0>{width}}" specifies that we want to fill the string with zeros (“0”), right-align the number (“>”), and specify the width using the variable “desired_width”.

Conclusion

In conclusion, there are different methods to add leading zeros to a number in Python. We have explored different methods to add leading zeros to a number like, zfill() method, string formatting, f-strings or rjust() method, it is important to understand how each method works and choose the one that best suits the needs.

Updated on: 31-May-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements