title: String to bytes in both Python 2 and 3
body: <p>My function needs to take input as either a string or binary data (e.g., read from a file). If it's a string, I want to convert this to raw data (<code>bytes</code> or <code>bytearray</code>).</p>
  
  <p>In Python 3, I can do <code>data = bytes(data, 'utf8')</code>. However, this fails in Python 2 as it only takes one argument. Vice versa, <code>data = bytes(data)</code> works in Python 2, but not in Python 3 as it complains about needing an encoding to work to.</p>
  
  <p>For the sake of argument, let's say that all input, if it comes as a string, is UTF-8 encoded. Is there then a better way to achieve what I'm looking for than the following monstrosity:</p>
  
  <pre><code>try:
    data = bytes(data, 'utf8')
  except:
    data = bytes(data)
  </code></pre>
  
  <p>n.b., <code>data.encode()</code> works in Py3, but fails in Py2 in the case that the string contains non-ASCII bytes.</p>