Python bytes.isalnum() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

bytes.isalnum() returns True if the bytes object is not empty and every byte is an ASCII letter (a-z, A-Z) or an ASCII digit (0-9). Otherwise it returns False.

Spaces, underscores, hyphens, control bytes, and all bytes from 128 to 255 make the result False. That is a quick way to check that an identifier received over the network uses only safe ASCII characters.

bytes.isalnum() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.isalnum(...). No import is needed, and bytearray objects have the same method.

Syntax

No arguments; returns a bool.

python
result = data.isalnum()
CallWhat it does
b"abc123".isalnum()Returns True.
b"abc_123".isalnum()Returns False: underscore.
b"".isalnum()Returns False: empty.

isalnum() is true exactly when every byte passes isalpha() or isdigit().

Method signatures
python
bytes.isalnum()
# returns True or False; bytearray.isalnum() works the same way

Parameters

isalnum() takes no parameters.

Return Value

Return ValueDescription
TrueAt least one byte, and all bytes are ASCII letters or digits.
FalseThe object is empty or contains any other byte.

Python Examples

Example 1 — Check Several Byte Strings

Run the check on plain tokens, a token with a space, empty bytes, encoded text, and a short code.

python
samples = [b"abc123", b"abc 123", b"", b"caf\xc3\xa9", b"X9"]

for s in samples:
    print(s, s.isalnum())

How It Works

  1. Letters and digits. b"abc123" and b"X9" contain nothing else, so they pass.
  2. A space breaks it. One space byte is enough to return False.
  3. Zero-length input. b'' returns False because the check needs at least one byte.
  4. UTF-8 fails. The two bytes of the accented e are outside ASCII.

Example 2 — Validate a Six-Byte Device ID

A device ID must be exactly six ASCII letters or digits. Combine a length check with isalnum().

python
def valid_id(raw):
    return len(raw) == 6 and raw.isalnum()


print(valid_id(b"AB12CD"))
print(valid_id(b"AB-2CD"))
print(valid_id(b"AB12C"))

How It Works

  1. Both rules pass. Six bytes, all letters or digits.
  2. Hyphen rejected. The length is right but - is not alphanumeric.
  3. Too short. and stops at the failed length check before calling isalnum().

Example 3 — bytes.isalnum() vs str.isalnum()

The same word gives different answers as text and as UTF-8 bytes. Underscores fail in both.

python
word = "caf\u00e9"

print(word.isalnum())
print(word.encode("utf-8").isalnum())
print(b"snake_case".isalnum(), b"2026".isalnum())

How It Works

  1. Unicode letters. str.isalnum() accepts the accented e as a letter.
  2. ASCII only for bytes. After encoding, the accented letter is two bytes above 127, so the check fails.
  3. Underscore and digits. _ is never alphanumeric; a digit-only object passes.

Frequently Asked Questions

That the bytes are not empty and every byte is an ASCII letter or digit.
All the is...() methods on bytes (except isascii()) require at least one byte.
Not in bytes. Decode to str and use str.isalnum() for Unicode letters.
No. b"a_b".isalnum() is False.
Remove them first: data.replace(b"_", b"").isalnum(), keeping in mind that an all-underscore value then becomes empty.

Next: bytes.isalpha()

True if all bytes are ASCII letters and there is at least one byte.

isalpha() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful