Write a Python program to print the following integers with zeros on the left of specified width


The program is written in Python and performs some basic string formatting operations. It first defines two variables a and b and assigns them the values 23 and 833 respectively. Then, it uses the print() function to display the original value of each variable along with its formatted representation. The formatted representation of the numbers is generated using the format() method on a string that contains a format specification.

The format specification has the following syntax: {:0>2d} and {:0>6d}. The 0 in the specification represents the padding character, > represents the justification of the output and 2 and 6 represent the width of the output. In this case, the output is left-justified and padded with zeros to achieve a width of 2 or 6 characters. The d specifies that the input is an integer.

Finally, the formatted string and the result of the format() method are concatenated using the + operator and passed as an argument to the print() function to display the final output.

Source Code

a = 23
b = 833
print("\nOriginal Number: ", a)
print("Formatted Number(left padding, width 2): "+"{:0>2d}".format(a));
print("Original Number: ", b)
print("Formatted Number(left padding, width 6): "+"{:0>6d}".format(b));

Output

Original Number:  23
Formatted Number(left padding, width 2): 23
Original Number:  833
Formatted Number(left padding, width 6): 000833


Example Programs