Remember
zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.
zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = '-42'
result = text.zfill(5)
print(result)Top-level scope
'-42'Object 2 · str'-0042'zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = '-42'
result = text.zfill(5)
print(result)
|
v
DISPLAYED RESULT
-0042
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: '-42'
object 2: '-0042'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = '-42'
result = text.zfill(5)
print(result)Top-level scope
'-42'Object 2 · str'-0042'The example has finished. Compare the final output with the values and references above.
-0042
What do you expect this program to print?
text = '-42'
result = text.zfill(5)
print(result)zfill() MethodThe zfill() method in Python is used to pad a numeric string with leading zeros, so that the resulting string reaches a specified length. It's especially useful when formatting numbers like invoice IDs, codes, or timestamps.
string.zfill(width)
width – The total length of the resulting string (including leading zeros).width characters long.text = "42"
print(text.zfill(5))
00042
text = "-42"
print(text.zfill(5))
-0042
Note: The negative sign is preserved in its original position.
text = "12345"
print(text.zfill(4))
12345
The string remains unchanged because its length is already more than the specified width.
INV000123)05 instead of 5zfill() on non-string types like integers will raise an error. Convert to string first.zfill() with rjust() or ljust() which pad with spaces by default.zfill() is handy in problems requiring fixed-width formatting or normalization of numeric string data before comparison.
zfill() pads the string on the left with 0s until it reaches the specified length.str() on numbers before calling zfill().Write a program that asks the user for a number and prints it as a 6-digit zero-padded string.
user_input = input("Enter a number: ")
print("Padded Number:", user_input.zfill(6))
Connect what you noticed in the example with the idea below.
zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.
zfill() inserts zeros before a leading minus sign.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.