Python bytes.isalpha() Method

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

Definition

bytes.isalpha() returns True if the object is not empty and every byte is an ASCII letter: A-Z (65-90) or a-z (97-122).

Digits, spaces, punctuation, and bytes 128-255 all make it False. It is useful for picking out word tokens from protocol text or log lines that also contain numbers and symbols.

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

Syntax

No arguments; returns a bool.

python
result = data.isalpha()
CallWhat it does
b"Python".isalpha()Returns True.
b"Py3".isalpha()Returns False.
b"\xc3\xa9".isalpha()Returns False: not ASCII.

To test a single byte, slice it (data[i:i + 1]), because indexing returns an int, which has no isalpha() method.

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

Parameters

isalpha() takes no parameters.

Return Value

Return ValueDescription
TrueAt least one byte, and all are ASCII letters.
FalseEmpty, or contains any non-letter byte.

Python Examples

Example 1 — Test Words, Codes, and Encoded Letters

Check plain words, a word with a digit, empty bytes, and one encoded accented letter.

python
for s in [b"Python", b"Py3", b"", b"\xc3\xa9", b"ABCxyz"]:
    print(s, s.isalpha())

How It Works

  1. Upper and lower. Both cases count, so b"ABCxyz" passes.
  2. Digit fails. The 3 in b"Py3" is not a letter.
  3. Empty and non-ASCII. Empty bytes and the two-byte accented e both return False.

Example 2 — Keep Only Word Tokens from a Log Line

Split a log line on whitespace and keep the tokens made only of letters.

python
line = b"ERR 404 not found at line 12"

words = [w for w in line.split() if w.isalpha()]
print(words)
print(len(words))

How It Works

  1. Tokens first. split() gives seven tokens.
  2. Numbers dropped. 404 and 12 fail the check.
  3. Five words. The remaining tokens are pure ASCII letters.

Example 3 — Test Each Byte Individually

Indexing gives ints, so slice one byte at a time, or compare the int value against the ASCII letter ranges.

python
data = b"A1b_"

print([data[i:i + 1].isalpha() for i in range(len(data))])
print([65 <= b <= 90 or 97 <= b <= 122 for b in data])

How It Works

  1. One-byte slices. data[i:i + 1] is a bytes object, so isalpha() can be called on it.
  2. Iterating gives ints. Looping over bytes yields values such as 65 for A.
  3. Same answer. The ranges 65-90 and 97-122 are exactly what isalpha() accepts.

Frequently Asked Questions

Only ASCII A-Z and a-z.
The check requires at least one byte.
Use a slice, data[i:i+1].isalpha(), because data[i] is an int.
No. b"hello world".isalpha() is False because of the space.
Decode the bytes to str and call str.isalpha().

Next: bytes.isascii()

True if every byte is in the ASCII range 0-127; True for empty bytes.

isascii() →

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.

9 people found this page helpful