zfill
Zero-Pad Numbers for Filenames That Sort Right
- STRING
"1", "2", "10" - sort those as text and you get 1, 10, 2, which is how you end up with a folder where frame 10 sorts before frame 2. zfill pads a string with leading zeros to a fixed width, so "7" with width 4 becomes "0007" and your filenames sort correctly forever. It's a small node with an outsized effect on your sanity.
How it works
Python's str.zfill(width) in a node, from StableLlama's Basic data handling pack - zero dependencies, pure Python. Two required inputs:
string(required) - the thing to pad, usually a number as text.width(required INT, default10, min0) - the total length you want the result to be.zfillpads with0characters on the left until the string reaches that length; if the string is already that long or longer, it's returned unchanged.
One output, STRING - the padded result. "42" with width 5 → "00042".
One detail worth knowing: it's called "zfill" because it pads with zeros, not spaces (that's ljust/rjust in the same pack, if you're padding with spaces instead). And there's a subtlety in the source: if the string starts with a + or - sign, the zeros go after the sign, so "-7" with width 3 becomes "-07", not "0-7". Handy if you're padding signed values.
Why you'd reach for it
- Batch filenames - turn frame or batch numbers into
0001.png,0002.png…0010.pngso they sort naturally. This is the classic use, and it pairs perfectly with the pack's time nodes for timestamped, zero-padded output names. - Fixed-width output - some tools, renderers, or APIs expect a fixed-width numeric string; zfill gives it to you without string math.
- Sorting - any list of numeric-looking strings that needs lexical order to match numeric order.
Because the input is a STRING, you can feed it a number that came out of a to STRING cast or a TimeFormat result. The pipeline "counter → string → zfill → filename" is common and dead simple.
Installing it
Search "Basic data handling" in ComfyUI Manager and restart, or:
cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
Restart and it's live. No models, no dependencies - the whole pack is plain Python.
Gotchas
It pads to reach the width - it doesn't cap it, so a width smaller than the input is silently ignored, and a width of 0 returns the string unchanged. And be careful padding things that aren't numbers: zfill on "abc" gives you "00abc" with width 5, which is technically valid and almost never what you meant. Use it on numeric text, not arbitrary strings.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| string | STRING | — | |
| width | INT | 10 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |