title: Python convert strings of bytes to byte array
body: <p>For example given an arbitrary string. Could be <code>chars</code> or just random <code>bytes</code>:</p>
  
  <pre><code>string = '\xf0\x9f\xa4\xb1'
  </code></pre>
  
  <p>I want to output:</p>
  
  <pre><code>b'\xf0\x9f\xa4\xb1'
  </code></pre>
  
  <p>This seems so simple, but I could not find an answer anywhere. Of course just typing the <code>b</code> followed by the string will do. But I want to do this runtime, or from a variable containing the strings of byte.</p>
  
  <p>if the given <code>string</code> was <code>AAAA</code> or some known <code>characters</code> I can simply do <code>string.encode('utf-8')</code>, but I am expecting the string of bytes to just be random. Doing that to <code>'\xf0\x9f\xa4\xb1'</code> ( random bytes ) produces unexpected result <code>b'\xc3\xb0\xc2\x9f\xc2\xa4\xc2\xb1'</code>.</p>
  
  <p>There must be a simpler way to do this?</p>
  
  <p>Edit:</p>
  
  <p>I want to convert the string to bytes without using an encoding</p>