0 votes
1 view
in Python by (47.8k points)


a = 1

b = 10

c = 100

How do I display a leading zero for all numbers with less than two digits?

That is,




1 Answer

0 votes
by (107k points)

In Python, we have a function called str.zfill which can be used to get value with leading zero:


This function returns the numeric string left filled with zeros in a string of length width. A sign prefix is handled correctly. The original string is returned if the width is less than or equal to the length of that string.

Following is the code that illustrates how we use it:-

print str(1).zfill(2)

print str(10).zfill(2)

print str(100).zfill(2)


Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !