In Python 3, use isinstance(value, bytes) to check whether a variable is a byte string. If your code should also accept mutable byte sequences, check for both bytes and bytearray.
Check for a Python 3 bytes object
Use isinstance() for the usual runtime type check:
if isinstance(value, bytes):
print("value is bytes")
This returns true for bytes instances and their subclasses. Use type(value) is bytes only when your API deliberately excludes subclasses.
Decide whether to accept bytearray
str represents text in Python 3; bytes represents immutable binary data; and bytearray is a separate, mutable binary sequence. Include bytearray only if your function accepts it:
if isinstance(value, (bytes, bytearray)):
print("value is bytes or bytearray")
The choice is about the input types your code promises to accept. A check for these concrete types is not a general test for every object that supports Python’s buffer protocol.
#1 Best Overall
Do not confuse checking with decoding
A type check does not convert data or establish that its contents are valid text in a particular encoding. Calling str() on bytes without an encoding produces a representation, such as "b'hello'", rather than decoding the bytes.
When you intend to read the bytes as text, decode them explicitly with the encoding appropriate to the data:
Rank #2
text = value.decode("utf-8")
Keep the operations separate: first decide which input types your code accepts; then decode bytes only when text is needed.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →




