Nathaniel Ford Nathaniel Ford - 23 days ago 4x
Python Question

How to identify a string as being a byte literal?

In Python 3+, if I have a string such that:


yields something like this:

b'This is the content of my string.\r\n'

I know it's a byte literal. Is there a function that can be used to determine if that string is in byte literal format (versus having, say, the Unicode 'u' 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 encoding in Unicode. In theory, something like this:

if is_byte_literal(some_str):
// handle byte literal case
// handle unicode case


The usual way to do this would be by utilizing an isinstance check with the bytes type:

if isinstance(some_str, bytes):
    pass # do bytes stuff
elif isinstance(some_str, str):
    pass # do str stuff

Since, a byte literal will always be an instance of bytes, isinstance will evaluate to True.