How to pad a string with leading zeros

How to pad a string with leading zeros

I have created an auto increment reference number but I like to display it with leading/padding zeros. So it looks like:

0001
0002
...
0256
0257

and so on.

This is the equivalent to the following printf function:

printf("d", variable)

How can I do the same in a deluge script?