title: Add leading zeroes to a string Python
body: <p>I've got an algorithm that does this already, but the code is ugly and slow. I assume that there's a built-in or some function in the libs that give faster and more precise results.</p>
  
  <p>What I want it where I can give a final number of bits, a string, list or deque (anything that can be cast to a string in the end).</p>
  
  <pre><code>func(totalBits, binaryInStringOrListForm) -&gt; String or List with leading zeroes
  </code></pre>
  
  <p>The code I've already got is below:</p>
  
  <pre><code> val = deque(["1", "0"])
  
       while len(val) &lt; 8:
  
           val.appendleft("0")
  </code></pre>
  
  <p>By the way, deque is a type of object from collections that allows you to <code>leftappend</code> wherein you can add an item to the beginning of the list without having to shift everything over by one.</p>
  
  <p>There are old answers to this but they're all Python 2 and the whole formatting thing has changed significantly. </p>
  
  <p>EDIT: I think that this question is not a duplicate of the one linked as I was actually after leading zeroes as @Martijn Pieters has made me aware.</p>
  
  <p>EDIT: Ok, it IS a duplicate of that one it seems. Sorry about the dupe.</p>
  
  <p>LAST EDIT I SWEAR: I just noticed something. I keep getting values like 000b0110 when trying to make it one-byte long. This is opposed to 00000110 that I wanted. I'm just adding this so that if anyone else in the future looks up this question they'll find the correct thing. So all I've done is ignore the first two character of the string by using:</p>
  
  <pre><code>val = val[2::].rjust(8, '0')
  </code></pre>
  
  <p>I'm getting this because I'm turning a number INTO a binary number with "bin" which returns a string that has the "0b" (declaring it as binary) at the beginning.</p>