October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Check Whether a Variable Is a Byte String in Python

Use isinstance(value, bytes) to check for a Python 3 byte string. Include bytearray only when your code accepts mutable byte sequences, and decode explicitly when you need text.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.