Dynamic Zero Padding.

Discussion in 'Python' started by Friedrich Clausen, Jun 7, 2011.

  1. Hello All,

    I want to print some integers in a zero padded fashion, eg. :

    >>> print("Testing %04i" % 1)

    Testing 0001

    but the padding needs to be dynamic eg. sometimes %05i, %02i or some
    other padding amount. But I can't insert a variable into the format
    specification to achieve the desirable padding.

    I would be much obliged if someone can give me some tips on how to
    achieve a variably pad a number.

    Cheers,

    Fred.

    Thanks,

    Fred.
    Friedrich Clausen, Jun 7, 2011
    #1
    1. Advertising

  2. Friedrich Clausen

    Mel Guest

    Friedrich Clausen wrote:
    > I want to print some integers in a zero padded fashion, eg. :
    >
    >>>> print("Testing %04i" % 1)

    > Testing 0001
    >
    > but the padding needs to be dynamic eg. sometimes %05i, %02i or some
    > other padding amount. But I can't insert a variable into the format
    > specification to achieve the desirable padding.
    >
    > I would be much obliged if someone can give me some tips on how to
    > achieve a variably pad a number.


    :)

    ('%%0%dd' % (pads,)) % (n,)

    Probably be good to wrap it in a function. It looks kind of obscure as it
    is.

    Mel.
    Mel, Jun 7, 2011
    #2
    1. Advertising

  3. Friedrich Clausen

    harrismh777 Guest

    Friedrich Clausen wrote:
    > I would be much obliged if someone can give me some tips on how to
    > achieve a variably pad a number.



    >>>> b='04'
    >>>> a="testing %"+b+"i"
    >>>> print(a % 1)

    > testing 0001





    kind regards,
    m harris
    harrismh777, Jun 7, 2011
    #3
  4. On Wed, Jun 8, 2011 at 7:43 AM, Mel <> wrote:
    > :)
    >
    > ('%%0%dd' % (pads,)) % (n,)
    >
    > Probably be good to wrap it in a function.  It looks kind of obscure asit
    > is.


    Would get rather pretty (read: ugly and impossible to read) if you
    wanted to put a literal percent sign in front of the number.

    :)

    Chris Angelico
    Chris Angelico, Jun 8, 2011
    #4
  5. Friedrich Clausen

    Terry Reedy Guest

    On 6/7/2011 7:05 PM, John Posner wrote:

    > You might want to try "new style" string formatting [1], which I think
    > is better than the "old style" in this particular case:
    >
    > >>> "Testing {0:0{1}d}".format(42, 4)

    > 'Testing 0042'
    > >>> "Testing {0:0{1}d}".format(42, 9)

    > 'Testing 000000042'


    One cannot use a nested field in the 'name' part of the field (before
    the ':' (I tried), but are pretty free to nest in the actual
    specification part after the ':'.

    >>> '{0:{1}}'.format(7,'b')

    '111'
    >>> '{0:{1}}'.format(7,'d')

    '7'

    --
    Terry Jan Reedy
    Terry Reedy, Jun 8, 2011
    #5
  6. On Wed, Jun 8, 2011 at 4:56 AM, Terry Reedy <> wrote:
    > On 6/7/2011 7:05 PM, John Posner wrote:
    >
    >> You might want to try "new style" string formatting [1], which I think
    >> is better than the "old style" in this particular case:
    >>
    >>     >>> "Testing {0:0{1}d}".format(42, 4)
    >>    'Testing 0042'
    >>     >>> "Testing {0:0{1}d}".format(42, 9)
    >>    'Testing 000000042'

    >
    > One cannot use a nested field in the 'name' part of the field (before the
    > ':' (I tried), but are pretty free to nest in the actual specification part
    > after the ':'.
    >
    >>>> '{0:{1}}'.format(7,'b')

    > '111'
    >>>> '{0:{1}}'.format(7,'d')

    > '7'


    Thanks all for the many good suggestions - I am looking over them and
    reading the referenced docs.

    Cheers,

    Fred.
    Friedrich Clausen, Jun 8, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zhiqiang Ye
    Replies:
    53
    Views:
    10,215
    Dan Pop
    Jun 28, 2004
  2. Gerard Flanagan
    Replies:
    3
    Views:
    428
    Terry Hancock
    Nov 19, 2005
  3. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    657
    Walter
    Jul 1, 2004
  4. mojumbo
    Replies:
    11
    Views:
    593
    James Kanze
    Oct 8, 2008
  5. Cal Who
    Replies:
    6
    Views:
    377
    Cal Who
    Nov 25, 2009
Loading...

Share This Page