bytes.isalpha and the is* family()
Eight predicates that share one rule set. The important difference from their str counterparts: these know only ASCII, so no accented or non-Latin byte ever counts as a letter.
Demo
Each predicate asks whether EVERY byte qualifies, so one bad byte makes the whole answer False. The two accented cases together tell the real story: "héllo" is not isalpha, because the accented character is stored as two non-ASCII bytes that are not letters as far as bytes is concerned — and it is not isascii either, for the same reason. The str versions of these methods answer True for both, which is exactly the gap to watch when converting text code to binary.
Common patterns
if not field.isdigit(): raise ValueError("expected digits")
if data.isascii(): text = data.decode('ascii')
records = [r for r in rows if not r.isspace()]
Examples
Pitfalls
'héllo'.encode().isalpha()
'héllo'.isalpha()
b''.isalpha(), b''.isascii()
if data and data.isalpha():
b'-5'.isdigit()
try: n = int(b'-5') except ValueError: ...
b'123'.islower(), b'123'.isupper()
b'a1'.islower()
b'Abc Def'.istitle()
data == data.title()
When to use
- Validating binary fields before parsing them
- A fast isascii check before decoding
- Rejecting blank or whitespace-only records
- The data is text → decode first; the str versions understand Unicode
- Deciding whether int() will succeed → just try the conversion
- Anything involving non-ASCII letters — these will always say no
Notes
FAQ
Because bytes knows nothing about Unicode. In UTF-8 an accented character is two bytes, neither of which is an ASCII letter, so the test fails. The str version decodes first and answers True.
'é'.encode().isalpha() # False 'é'.isalpha() # True