title: Formatting number as fixed-length binary in python when number is larger than number of bits
body: <p>I am trying to convert a number to its binary representation with a fixed length. I have tried using </p>
  
  <pre><code>&gt;&gt;&gt; "{0:04b}".format(number)
  </code></pre>
  
  <p>which works when the number can be fully represented in four bits (0-15), however I need the string to always be four bits long, even if the number is out of range. For example,</p>
  
  <pre><code>&gt;&gt;&gt; "{0:04b}".format(17)
  </code></pre>
  
  <p>should return '0001', not '10001'.</p>
  
  <p>I am aware I can just index the last 4 bits of the resulting string, however I am wondering if there is a more elegant solution.</p>