zfill(width) — Python Technical Interview Questions & Answers

zfill(width)

zfill(width)

  • zfill is a string helper. You call it on text like "Asha" or "banana". It gives a new string (or a number/True-False). The old string does not change unless you assign the result back.
  • zfill(width) — output: 007.
  • Fresher trap: writing s.zfill() and expecting s to update. Strings are immutable. Write s = s.zfill(...) if you need the new value (when it returns a string).

Technical Interview Tip: Call zfill on one real word and say what prints.

Code Examples for zfill(width)

Example

# zfill(width)
print("7".zfill(3))

zfill(width) — output: 007.

How to Explain zfill(width) to an Interviewer

Explain zfill(width) as if you are teaching a junior — definition, then one tiny script.

zfill works on str. zfill usually returns a new string.

What does the example print, and why?

zfill(width) — output: 007

What mistake do freshers make with zfill(width)?

zfill does not change the original string in place.