How to identify a string as being a byte literal?
<p>In Python 3, if I have a string such that:</p>

<pre><code>print(some_str)
</code></pre>

<p>yields something like this:</p>

<pre><code>b'This is the content of my string.\r\n'
</code></pre>

<p>I know it's a byte literal. </p>

<p>Is there a function that can be used to determine if that string is in byte literal format (versus having, say, the Unicode <code>'u'</code> prefix) without first interpreting? Or is there another best practice for handling this? I have a situation wherein getting a byte literal string needs to be dealt with differently than if it's in Unicode. In theory, something like this:</p>

<pre><code>if is_byte_literal(some_str):
    // handle byte literal case
else:
    // handle unicode case
</code></pre>

<p>Just to complement the other answer, the built-in <a href="https://docs.python.org/3/library/functions.html#type" rel="nofollow"><code>type</code></a> also gives you this information. You can use it with <code>is</code> and the corresponding type to check accordingly.</p>

<p>For example, in Python 3:</p>

<pre><code>a = 'foo'
print(type(a) is str)   # prints `True`
a = b'foo'
print(type(a) is bytes) # prints `True` as well
</code></pre>

python string python-3.x