Suggested coding style

Ian Kelly ian.g.kelly at gmail.com
Thu Sep 29 18:12:51 EDT 2011


On Thu, Sep 29, 2011 at 6:23 AM, rantingrick <rantingrick at gmail.com> wrote:
> A specific method for padding a string with ONLY zeros is ludicrous
> and exposes the narrow mindedness of the creator. The only thing worse
> than "zfill" as a string method is making zfill into built-in
> function! The ONLY proper place for zfill is as an option in the
> str.format() method.
>
> py> "{0:zf10}".format(1234) -> "00000000001234"

Agree that zfill seems to be redundant with str.format, although your
suggested syntax is atrocious, especially since a syntax already
exists that fits better in the already-complicated format specifier
syntax.

"{0:=010d}".format(1234) -> "0000001234"

There are a couple of warts with the existing implementation, however:

1) str.zfill() operates on strings; the .format() syntax operates on
numeric types.  I would suggest that the "=" fill alignment in format
specifiers should be extended to do the same thing as zfill when given
a string.

2) It seems to not behave as documented for floats.  I expect:

"{0:=010f}".format(-32.7) -> "-0000032.7"

I get:

"{0:=010f}".format(-32.7) -> "-32.700000"

On the other hand, I can't imagine why I would ever actually want the
expected result, so maybe it's not a big deal.



More information about the Python-list mailing list