zfill() Method: String में Leading Zeros Add करना
zfill Method String में Left Side Zeros Pad करने के लिए बनाया गया है।
उदाहरण
>>> '25'.zfill(6) '000025'
Alternative: rjust() Method
हम String Class के rjust(width[, fillchar]) Method का भी उपयोग कर सकते हैं जो String को Right Justify करता है और Left Side को Given Filler Char से Pad करता है। Default Filler Char Space होता है लेकिन हम इसे '0' भी Provide कर सकते हैं।
>>> '25'.rjust(6, '0') '000025'
String Formatting का उपयोग
हम Python String Formatting का उपयोग करके भी यही Result प्राप्त कर सकते हैं:
>>> print("%06d" % 25)
'000025'
Modern Formatting (Python 3.6+):
>>&>> f"{25:06d}"
'000025'
>>> format(25, '06d')
'000025'
Comparison:
| Method | Example | Output |
|---|---|---|
zfill(6) |
'25'.zfill(6) | '000025' |
rjust(6, '0') |
'25'.rjust(6, '0') | '000025' |
%06d |
'%06d' % 25 | '000025' |
f-string |
f"{25:06d}" | '000025' |
Note: zfill Sign को Handle करता है: '-25'.zfill(6) देता है '-00025'।