Suggested coding style

rantingrick rantingrick at gmail.com
Fri Sep 30 01:47:40 CEST 2011


On Sep 29, 5:12 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> On Thu, Sep 29, 2011 at 6:23 AM, rantingrick <rantingr... 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.

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.



More information about the Python-list mailing list