Suggested coding style

Ian Kelly ian.g.kelly at gmail.com
Fri Sep 30 06:49:43 CEST 2011


On Thu, Sep 29, 2011 at 5:47 PM, rantingrick <rantingrick at gmail.com> wrote:
> Ah ha! Found the answer!
>
> py> "{0:010d}".format(1234)
> 0000001234
>
> py> "{0:0>10}".format(1234)
> 0000001234
>
> py> "{0:0>10}".format("1234")
> 0000001234
>
> py> "{0:@>10}".format("1234")
> @@@@@@1234
>
> I would skip using the "{int}{repeat}d" syntax and just use the string
> padding since you won't need to worry about the input data type. I
> hate specificity types in string formats. With the old interpolation i
> ALWAYS used %s for everything.

Nope, that doesn't work.

>>> "{0:0>10}".format("-1234")
'00000-1234'

The whole point of zfill is that it handles signs correctly.



More information about the Python-list mailing list