Categories
python string zero-padding

How do I pad a string with zeroes?

1922

How do I pad a numeric string with zeroes to the left, so that the string has a specific length?

0

    3072

    To pad strings:

    >>> n = '4'
    >>> print(n.zfill(3))
    004
    

    To pad numbers:

    >>> n = 4
    >>> print(f'{n:03}') # Preferred method, python >= 3.6
    004
    >>> print('%03d' % n)
    004
    >>> print(format(n, '03')) # python >= 2.6
    004
    >>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
    004
    >>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
    004
    >>> print('{:03d}'.format(n))  # python >= 2.7 + python3
    004
    

    String formatting documentation.

    7

    • 9

      Comments python >= 2.6 are incorrect. That syntax doesn’t work on python >= 3. You could change it to python < 3, but may I suggest instead always using parenthesis and omitting the comments altogether (encouraging recommended usage)?

      Sep 28, 2015 at 13:13

    • 5

      Note that you don’t need to number your format strings: '{:03d} {:03d}'.format(1, 2) implicitly assigns the values in order.

      – Dragon

      Jul 8, 2016 at 11:32

    • 1

      @JasonR.Coombs: I assume you meant the print statement, when it should be a print function on Python 3? I edited in the parens; since only one thing is being printed, it works identically now on Py2 and Py3.

      Jan 25, 2019 at 2:19

    • 3

      Can any of these approaches be adapted to work with a variable number of zeroes?

      May 21, 2019 at 22:04

    • 8

      How could you have not used the number 7 for your example?!? 😲

      – Motti

      Aug 13, 2019 at 8:54

    442

    Just use the rjust method of the string object.

    This example creates a 10-character length string, padding as necessary:

    >>> s="test"
    >>> s.rjust(10, '0')
    >>> '000000test'
    

    4

    • In my opinion, it should be ‘t = t.rjust(10, ‘0’), otherwise the value of t remains unchanged (at least for me)

      Mar 10 at 8:50

    • 1

      @StanislavKoncebovski strings are immutable in Python. The value of a string will always remain unchanged no matter what you do to it and you always have to reassign if you want to update the variable to reference the new string. This has nothing to do with rjust.

      – Neil

      Apr 13 at 9:59

    • never heard of that method before – good at hiding but now it has been “out’ed” !

      Apr 21 at 14:07


    • @Paul D.Eden You may be right theoretically, but I checked it again, and yes, if you do not assign like t = t.rjust(10, ‘0’) you will not obtain ‘000000test’ in t. My assertion is based on a test. I am using Python 3.7.

      Apr 22 at 17:18

    183

    Besides zfill, you can use general string formatting:

    print(f'{number:05d}') # (since Python 3.6), or
    print('{:05d}'.format(number)) # or
    print('{0:05d}'.format(number)) # or (explicit 0th positional arg. selection)
    print('{n:05d}'.format(n=number)) # or (explicit `n` keyword arg. selection)
    print(format(number, '05d'))
    

    Documentation for string formatting and f-strings.

    2

    • @Konrad: “The documentation, however, says to use format instead.” I know I’m late to the party, but I’d like to see what you mean by this. The documentation I see (docs.python.org/3/library/stdtypes.html#old-string-formatting) says using format or other alternatives “may help avoid [aforementioned] errors” associated with % interpolation. That’s not very robust “deprecation.”

      – LarsH

      Jun 11, 2019 at 22:33


    • @LarsH Well it’s notable that the link in my answer originally pointed to % formatting. It now points to str.format formatting. I did not change the link! Rather, the Python documentation website behind that link was rewritten. Apart from that, the documentation used to have stronger wording, and literally states that str.format “should be preferred to the % formatting”, just as I wrote in the comment you quoted.

      Jun 11, 2019 at 23:25