2

Probably a silly question, but in python is there a simple way to automatically pad a number with zeros to a fixed length? I wasn't able to find this in the python docs, but I may not have been looking hard enough? e.i. I want bin(4) to return 00100, rather than just 100. Is there a simple way to ensure the output will be six bits instead of three?

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144

3 Answers3

11

Strings have a .zfill() method to pad it with zeros:

>>> '100'.zfill(5)
'00100'

For binary numbers however, I'd use string formatting:

>>> '{0:05b}'.format(4)
'00100'

The :05b formatting specification formats the number passed in as binary, with 5 digits, zero padded. See the Python format string syntax. I've used str.format() here, but the built-in format() function can take the same formatting instruction, minus the {0:..} placeholder syntax:

>>> format(4, '05b')
'00100'

if you find that easier.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Ah, fantastic! Just what I was looking for. Bonus points for including both solutions, which is why once this one is accepted over others. – Slater Victoroff Nov 28 '12 at 07:14
  • @SlaterTyranus: `bin/zfill` is slightly faster than `format` in [my measurements](http://stackoverflow.com/questions/12161988/multiplying-a-huge-number-times-random-python#comment16331044_12162856). – jfs Apr 26 '15 at 01:48
  • @J.F.Sebastian: I see no measurements for `format()`, only `str.format()`. `str.format()` requires a parsing step, then filling the slots, which internally then uses `format()`. To compare with `bin/zfill`, you need to not incur that extra cost. – Martijn Pieters Apr 26 '15 at 01:51
  • @MartijnPieters: yes. My older comment uses `"".format` that hints at that. Though I don't know what is "fair" translation of: `"{:0{}b}".format(n, len(data)*8)` (it is compared against `bin(n)[2:].zfill(len(data)*8)`) using only `format()` e.g., `format(n, "0%db" % (len(data)*8))` has performed **worse**. – jfs Apr 26 '15 at 02:20
  • @J.F.Sebastian: The length cannot be pre-computed? Why not compare `bin()/zfill()` and `format()` with a fixed length? – Martijn Pieters Apr 26 '15 at 02:23
  • @MartijnPieters: `data` is the input for [the functions that are compared](https://gist.github.com/zed/3526111#file-test_b2a_bin-py-L16) -- it can't be eliminated. The only benchmark that matters is your application on the hardware it is deployed; that Is why I said *"my measurements"* – jfs Apr 26 '15 at 02:31
  • @J.F.Sebastian: but if you are going to use those measurements to make general statements about performance then you need to either state those constraints or re-do them for the more general context. :-) – Martijn Pieters Apr 26 '15 at 02:33
  • 1. I've stated it twice already. Here's the 3rd time to be sure: *"in my measurements"* 2. I'm not sure my use case is less relevant for @SlaterTyranus than a static format case. And I'm not sure it is valid to call a static length to be more general than a variable length. 3. And `format()` is still slightly slower than `bin/zfill` even with static length *on examples that I've tried by hand*. :) – jfs Apr 26 '15 at 02:49
  • @J.F.Sebastian: yeah, I hadn't missed the *in my measurements* part. :-) – Martijn Pieters Apr 26 '15 at 02:50
3

try this...

In [11]: x = 1

In [12]: print str(x).zfill(5)
00001

In [13]: bin(4)
Out[13]: '0b100'

In [14]: str(bin(4)[2:]).zfill(5)
Out[14]: '00100'
avasal
  • 14,350
  • 4
  • 31
  • 47
3

This is a job for the format built-in function:

>>> format(4, '05b')
'00100'
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485