w3resource

Python: Print the following integers with zeros on the left of specified width

Python String: Exercise-33 with Solution

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

Python String Exercises: Print the following integers with zeros on the left of specified width

Sample Solution:

Python Code:

# Define a variable 'x' and assign it the value 3 (an integer).
x = 3

# Define a variable 'y' and assign it the value 123 (an integer).
y = 123

# Print an empty line for spacing.
print()

# Print the original value of 'x' with a label.
print("Original Number: ", x)

# Format the value of 'x' with left padding using zeros to a width of 2 characters and print it.
print("Formatted Number (left padding, width 2): "+"{:0>2d}".format(x))

# Print the original value of 'y' with a label.
print("Original Number: ", y)

# Format the value of 'y' with left padding using zeros to a width of 6 characters and print it.
print("Formatted Number (left padding, width 6): "+"{:0>6d}".format(y))

# Print an empty line for spacing.
print()

Sample Output:

Original Number:  3                                                                                           
Formatted Number(left padding, width 2): 03                                                                   
Original Number:  123                                                                                         
Formatted Number(left padding, width 6): 000123 

Flowchart:

Flowchart: Print the following integers with zeros on the left of specified width

Python Code Editor:

Previous: Write a Python program to print the following floating numbers with no decimal places.
Next: Write a Python program to print the following integers with '*' on the right of specified width.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-33.php