String variable as literal bytes
<p>I'm reading in a config file. Say I end up with a configuration variable:</p>

<pre><code>header = '\x42\x5a\x68'
</code></pre>

<p>I want to match this against binary files using startswith.</p>

<p>Unsurprisingly, I get a <code>"TypeError startswith first arg must be bytes or a tuple of bytes, not str"</code>, if I try to use this directly. How do I use this string? I don't want it encoded.</p>

<p>I have to read the string from a file. If there's some other way to go about this, I'm all ears. Thanks for reading!</p>

<blockquote>
  <p>How do I use this string? I don't want it encoded.</p>
</blockquote>

<p>You must. Make a decision, either <code>decode</code> the string you get from the binary file or <code>encode</code> the <code>header</code> name. You can't mix those two types together.</p>

<p>See <em><a href="https://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit" rel="nofollow">Text Vs. Data Instead Of Unicode Vs. 8-bit</a></em> in the python docs, all mixing between these types (as it occurs in <code>startswith</code> too) will result in a <code>TypeError</code>.</p>

python string python-3.x