Horje
How to Add leading Zeros to a Number in Python

In this article, we will learn how to pad or add leading zeroes to the output in Python.

Example:

Input: 11
Output: 000011
Explanation: Added four zeros before 11(eleven).

Display a Number With Leading Zeros in Python

Add leading Zeros to numbers using format()

For more effective handling of sophisticated string formatting, Python has included the format() method. A formatter inserts one or more replacement fields and placeholders into a string by using the str.format function and a pair of curly braces ().

Python3

print("{:03d}".format(1))
 
print("{:05d}".format(10))

Output

001
00010

Add leading Zeros to the number using f-string

Prefix the string with the letter “f” to get an f-string. The string itself can be prepared similarly to how str.format would be used (). F-strings offer a clear and practical method for formatting python expressions inside of string literals.

Python3

num1 = 16
 
print(f" {num1 : 03d} ")
print(f" {num1 : 07d} ")

Output

  16 
  000016 

Add zeros using  rjust() method :

One approach that is not mentioned in the article is using the rjust() method of the built-in str class to add leading zeros to a number. The rjust() method returns a right-justified string, with optional padding, based on a specified width.

Here is an example of how you could use the rjust() method to add leading zeros to a number:

Python3

num = 11
padded_num = str(num).rjust(6, '0')
print(padded_num)  # Output: 000011

Output

000011

Using the rjust() method can be a simple and concise way to add leading zeros to a number, especially if you only need to pad the number with a fixed number of zeros. It is also useful if you need to pad a number that is stored as a string, as it works directly on strings.

Note that the rjust() method returns a new string, so you will need to store the padded number in a separate variable if you want to keep it. 

Add zeros using  zfill() method :

The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length. If the value of the len parameter is less than the length of the string, no filling is done.

Below is the implementation:

Python3

num = '42'
ans = num.zfill(8)
print(ans)

Output

00000042

Method :Using the % operator with the 0 flag:

Python3

num = 42
res = "%06d" % num
print(res)

Output

000042

Add zeros using join() method :

  • Converting the number to a string using the str()
  • Finding the length of the string using the len().
  • The join() method is commonly concatenate a sequence of strings with a specified separator
  • Concatenating the list of zeros and the original string using the + operator
  • Assigning the result to padded_num

Python3

num = 11
padded_num = "".join(["0"] * (6 - len(str(num)))) + str(num)
print(padded_num)  # Output: 000011

Output

000011

Time complexity: O(n), where n is the number of zeros to be added.

Space Complexity: O(n), where n is the number of zeros to be added.




Reffered: https://www.geeksforgeeks.org


Python

Related
Remove all columns where the entire column is null in PySpark DataFrame Remove all columns where the entire column is null in PySpark DataFrame
Install Packages Using PIP With requirements.txt File in Python Install Packages Using PIP With requirements.txt File in Python
Scraping Flipkart Data using Python Scraping Flipkart Data using Python
Python - Matrix Python - Matrix
Python program to remove last element from set Python program to remove last element from set

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11