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()
Call
What 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 Value
Description
True
At least one byte, and all are ASCII letters.
False
Empty, 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())