Parcel Label Padding with zfill()
A packing desk is printing shipment labels, and the scanner expects every parcel code to have the same width. Some codes already arrive with enough digits, but shorter ones need leading zeroes before they are printed.
The input for this problem is a parcel code stored as a string and a target width. If the code is shorter than that width, add "0" characters to the front until the total length matches. If the code is already wide enough, leave it unchanged.
For example, if code = "42" and width = 5, the printed label should be "00042". If code = "1205" and width = 3, the result stays "1205" because nothing needs to be added. A one-digit code like "7" with width 4 becomes "0007".
This is a formatting task, not a numeric calculation. The parcel code must stay a string, and the answer should preserve every original character while only adding zeroes at the front when needed.
Example Input & Output
Three leading zeroes are added so the label reaches width five.
The code is already longer than the requested width, so it stays the same.
A short one-digit code is left-padded until it fits the label printer format.
Algorithm Flow

Solution Approach
This is one of those cases where Python already gives you the exact behavior you want. Because the parcel code is a string and the formatting rule is specifically about adding zeroes on the left, the built-in zfill() method is the cleanest tool for the job.
The nice part about zfill() is that it expresses the intent directly. You are not really doing arithmetic here. You are not converting the code to an integer, measuring digits manually, or building a loop that pushes one "0" at a time. You simply want a string padded on the left until it reaches a certain width.
That means the whole solution can be written as:
When Python evaluates that line, it checks the current length of code. If the string is shorter than width, Python adds the needed number of leading zeroes. If the string is already long enough, it returns the original text unchanged.
That behavior matches the problem exactly. "42".zfill(5) becomes "00042". "1205".zfill(3) stays "1205". Even an empty string works in a predictable way: "".zfill(3) becomes "000".
You could recreate the same effect manually with something like "0" * needed + code, where needed is the difference between the target width and the current length. That would work, but it is longer and makes the reader reconstruct the formatting rule from smaller pieces. In Python, zfill() already packages that rule into one very readable method call.
So the key idea here is not clever algorithm design. It is recognizing that this is a string-formatting problem and choosing the standard Python method that was built for exactly this situation. The runtime is linear in the size of the resulting string, and the code stays short, clear, and appropriately Pythonic.
Best Answers
def format_parcel_code(code, width):
if len(code) >= width:
return code
return "0" * (width - len(code)) + codeComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
