Note
Python - isalpha() - Check if a string contains only letters
Overview of the Python isalpha() Function: Check if a String Contains Only Letters
string.isalpha() is a method that we can call on string, byte, and bytearray objects.
It checks if a given object contains only letters or rather - alphabetic characters.
Parameters:
value: The element to check for alphabetic characters.
This method returns a boolean (True or False) depending on the object contents.
It is worth noting that special characters existing in some languages are also considered alphabetic
characters. So they won't trigger a False return by themselves.
Spaces, numbers, and symbols will cause the method to return False.
Take a look at the examples below:
Using isalpha() with Strings
string_1 = "DevCuriosity"
string_2 = "ąàêöçÂÁÀÀĆāيঅਅҐЄ漢위កกⴲ" # These are special characters from various languages
string_3 = "Dev Curiosity"
string_4 = "313"
string_5 = "www.DevCuriosity.com"
print(string_1.isalpha()) # True
print(string_2.isalpha()) # True (special characters from different languages are also considered as alphabetic characters)
print(string_3.isalpha()) # False (because it contains a space)
print(string_4.isalpha()) # False (because of non-alphabetic characters - numbers)
print(string_5.isalpha()) # False (because of non-alphabetic characters - dots)
Output:
True
True
False
False
False
More special cases of isalpha()
mixed_string = "abc1"
empty_string = ""
print(mixed_string.isalpha()) # False (contains numbers)
print(empty_string.isalpha()) # False (empty string)
Output:
False
False
As presented, isalpha() will return False when we have even one digit among letters.
False will also be return when called on empty strings!
Using isalpha() with Bytes
In byte values, everything works as expected but special characters are impossible to assign to a byte type because they are not ASCII characters:
bytes_1 = b"DevCuriosity"
# bytes_2 = b"ąàêöçÂÁÀÀĆāيঅਅҐЄ漢위កกⴲ" # Here we would receive SyntaxError: bytes can only contain ASCII literal characters
bytes_3 = b"Dev Curiosity"
bytes_4 = b"313"
bytes_5 = b"www.DevCuriosity.com"
print(bytes_1.isalpha()) # True
print(bytes_3.isalpha()) # False (because it contains a space)
print(bytes_4.isalpha()) # False (because of non-alphabetic characters - numbers)
print(bytes_5.isalpha()) # False (because of non-alphabetic characters - dots)
Output:
True
False
False
False
Using isalpha() with Bytearrays
Same with bytearray:
bytearray_1 = bytearray(b"DevCuriosity")
# bytearray_2 = bytearray(b"ąàêöçÂÁÀÀĆāيঅਅҐЄ漢위កกⴲ") # Here we would receive SyntaxError: bytes can only contain ASCII literal characters
bytearray_3 = bytearray(b"Dev Curiosity")
bytearray_4 = bytearray(b"313")
bytearray_5 = bytearray(b"www.DevCuriosity.com")
print(bytearray_1.isalpha()) # True
print(bytearray_3.isalpha()) # False (because it contains a space)
print(bytearray_4.isalpha()) # False (because of non-alphabetic characters - numbers)
print(bytearray_5.isalpha()) # False (because of non-alphabetic characters - dots)
Output:
True
False
False
False
Key Points to Remember about the isalpha() Function
-
Compatibility:
isalpha()works with Strings and Byte-Like Objects -
Returns False for Non-Alphabetic Characters: Spaces, numbers, and symbols cause the method to return False.
-
Case Sensitivity: Alphabetic characters can be uppercase or lowercase.
-
Empty Strings: Calling
isalpha()on an empty string or byte object will returnFalse.
I hope you found this helpful! If you have any feedback, questions or requests, please reach out to me through Contact page!