How to display Number with leading zero in Python

To displaynumber with a leading zero in Python,

  1. rjust(): It will right-align a string using the specified character.
  2. zfill(): The zfill() method adds zeros (0) at the beginning of the string.
  3. String formatting using modulo: You can format a string that displays a number with a leading zero.
  4. format(): It formats the specified values and inserts them inside the string’s placeholder.
  5. f-string: The f-strings are a great new way to format strings.

Python rjust()

The rjust() is a built-in Python function that adds padding to a string’s left or right side. We can use the rjust() function to add zero at the beginning of the string.

The rjust() function has two parameters: the width and the second is a fillchar representing the value we have to put in the string. Width is mandatory, which specifies the length of the series after padding.

Example

lst = [x for x in range(1, 100+1)] # it create list if 100 values
new_lst = list()

for i in lst: 
  # adds zero at starting of the string if needs.
  new_lst.append(((str(i).rjust(3, '0'))))

print(new_lst)

Output

['001', '002', '003', '004', '005', '006', '007', '008', '009', '010', 
 '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', 
 '021', '022', '023', '024', '025', '026', '027', '028', '029', '030', 
 '031', '032', '033', '034', '035', '036', '037', '038', '039', '040', 
 '041', '042', '043', '044', '045', '046', '047', '048', '049', '050', 
 '051', '052', '053', '054', '055', '056', '057', '058', '059', '060', 
 '061', '062', '063', '064', '065', '066', '067', '068', '069', '070', 
 '071', '072', '073', '074', '075', '076', '077', '078', '079', '080', 
 '081', '082', '083', '084', '085', '086', '087', '088', '089', '090', 
 '091', '092', '093', '094', '095', '096', '097', '098', '099', '100']

Here, we provide the length of a string as 3 and the filling character as 0. So, all character with a length of 1 or 2 is padded with 2 or 1 zero accordingly.

Python zfill()

The zfill() is a built-in Python function that fills zero into a value and returns a numeric value. It automatically fills zero into the left side of the value accordingly to width size. You can use the zfill() function to display the leading number with zero.

Example

lst = [x for x in range(1, 100+1)] # it create list if 100 values
new_lst = list()

for i in lst:
  # adds zero at starting of the string if needs.
  new_lst.append(((str(i).zfill(3))))
print(new_lst)

Output

['001', '002', '003', '004', '005', '006', '007', '008', '009', '010', 
'011', '012', '013', '014', '015', '016', '017', '018', '019', '020', 
'021', '022', '023', '024', '025', '026', '027', '028', '029', '030', '
031', '032', '033', '034', '035', '036', '037', '038', '039', '040', 
'041', '042', '043', '044', '045', '046', '047', '048', '049', '050', 
'051', '052', '053', '054', '055', '056', '057', '058', '059', '060', 
'061', '062', '063', '064', '065', '066', '067', '068', '069', '070', 
'071', '072', '073', '074', '075', '076', '077', '078', '079', '080', 
'081', '082', '083', '084', '085', '086', '087', '088', '089', '090', 
'091', '092', '093', '094', '095', '096', '097', '098', '099', '100']

You can see that we created a list filled with elements leading with zero.

String formatting (using modulo (%))

The % modulo symbol, also known as the string formatting operator, is one of several ways Python can implement string formatting. As the oldest way to implement string formatting, it works seamlessly with almost every version of Python currently available on the web.

Example

lst = [x for x in range(1, 50+1)] # it create list if 100 values
new_lst = list()

for i in lst:
 # adds zero at starting of the string if needs.
 new_lst.append(("%03d" % (i)))

print(new_lst)

Output

['001', '002', '003', '004', '005', '006', '007', '008', '009', '010', 
'011', '012', '013', '014', '015', '016', '017', '018', '019', '020', 
'021', '022', '023', '024', '025', '026', '027', '028', '029', '030', 
'031', '032', '033', '034', '035', '036', '037', '038', '039', '040', 
'041', '042', '043', '044', '045', '046', '047', '048', '049', '050']

Here, 0 is the padding value, and 3 is the width after padding the number. It is used for type conversion.

Python format()

The format() is a built-in Python method that formats the specified values and inserts them inside the string’s placeholder. Use curly braces {}  to show where to substitute a variable in the print statement. It is available in a newer version of Python (after 2.6).

lst = [x for x in range(1, 20+1)] # it create list if 100 values
new_lst = list()

for i in lst:
 
 # adds zero at starting of the string if needs.
 new_lst.append(("{:03d}").format(i))

print(new_lst)

Output

['001', '002', '003', '004', '005', '006', '007', '008', '009', '010', 
'011', '012', '013', '014', '015', '016', '017', '018', '019', '020']

After the colon, we specified a padding value and width of the string and the type conversion value in this code.

Python f-string

Implementing string formatting is more efficient than the other two previous methods, the % operator and the format () function. This is because it is faster and easier to understand. It also helps you implement string formats in Python faster than the other two.

Literal String Interpolation or, more commonly, F-strings. The main idea behind f-strings is to make string interpolation simpler.

Example

lst = [x for x in range(1, 10+1)] # it create list if 100 values
new_lst = list()

for i in lst:
 # adds zero at starting of the string if needs.
 new_lst.append((f"{i:03d}"))
print(new_lst)

Output

['001', '002', '003', '004', '005', '006', '007', '008', '009', '010']

In this code, before the colon, we need to specify the value we want to print, and after the colon, specifies the padding value and width of the sting along with the type conversion value.

That’s it for this tutorial.

 

Leave a Comment