title: How to read a string in a bitstream in Python
body: <p>I'm writing the following string in my output file:</p>
  
  <pre><code>bitstream.add("stri", 32)
  </code></pre>
  
  <p>where</p>
  
  <pre><code>def add(self, data, length):
      s = ''
      if (type(data) == str):
          for char in data:
              b = bin(ord(char))[2:]
              s = s + "{:0&gt;8}".format(b)
      else:
          s = bin(data)[2:]
          if (len(s) &lt; length):
              resto = length - len(s)
              for _ in range(0, resto):
                  s = '0' + s
      s = s[0:length]
      self.cache = self.cache + s
      self.flush()
  </code></pre>
  
  <p>Later on I need to read the string from the output file. I use Python <code>struct unpack</code> module as follows:</p>
  
  <pre><code>   from struct import unpack
     key_length_bytes = 32
     key = ""
     for _ in range(0, key_length_bytes):
        carattere = chr(unpack('&gt;B', in_file.read(1))[0])
        key = "%s%s" (key, carattere)
  </code></pre>
  
  <p>I get </p>
  
  <pre><code>key = "%s%s" (key, carattere)
  TypeError: 'str' object is not callable
  </code></pre>
  
  <p>Thank you for any help you could provide.</p>