title: Why can't Python's string.format pad with "\x00"?
body: <p>I wanted to pad a string with null characters ("\x00"). I know lots of ways to do this, so please do not answer with alternatives. What I want to know is: Why does Python's <code>string.format()</code> function not allow padding with nulls?</p>
  
  <p>Test cases:</p>
  
  <pre><code>&gt;&gt;&gt; "{0:\x01&lt;10}".format("bbb")
  'bbb\x01\x01\x01\x01\x01\x01\x01'
  </code></pre>
  
  <p>This shows that hex-escaped characters work in general.</p>
  
  <pre><code>&gt;&gt;&gt; "{0:\x00&lt;10}".format("bbb")
  'bbb       '
  </code></pre>
  
  <p>But "\x00" gets turned into a space ("\x20").</p>
  
  <pre><code>&gt;&gt;&gt; "{0:{1}&lt;10}".format("bbb","\x00")
  'bbb       '
  &gt;&gt;&gt; "{0:{1}&lt;10}".format("bbb",chr(0))
  'bbb       '
  </code></pre>
  
  <p>Even trying a couple other ways of doing it.</p>
  
  <pre><code>&gt;&gt;&gt; "bbb" + "\x00" * 7
  'bbb\x00\x00\x00\x00\x00\x00\x00'
  </code></pre>
  
  <p>This works, but doesn't use <code>string.format</code></p>
  
  <pre><code>&gt;&gt;&gt; spaces = "{0: &lt;10}".format("bbb")
  &gt;&gt;&gt; nulls  = "{0:\x00&lt;10}".format("bbb")
  &gt;&gt;&gt; spaces == nulls
  True
  </code></pre>
  
  <p>Python is clearly substituting spaces (<code>chr(0x20)</code>) instead of nulls (<code>chr(0x00)</code>).</p>