What is String.isalpha in Python?
The isalpha method will return True if all the characters of the string are alphabetic; otherwise, it returns False. If the string is empty, then the isalpha method will return False.
isalphawill returnTrueif all the characters of the string are defined in theCategory of the Unicode character Database. Refer here for Unicode Character categories. LetterLetter (L): lowercase (Ll), titlecase (Lt), uppercase (Lu), modifier (Lm), other (Lo)
Syntax
string.isalpha()
Return value
-
True: If all the characters of the string are alphabetic. -
False: If any one of the characters of the string is not alphabetic or if the string is an empty string.
Example
string = "text"print(string.isalpha())#isalpha returns false because string has numbersstring = "ab3"print(string.isalpha())#isalpha returns false because the string has spacesstring = "I have space"print(string.isalpha())#for empty string isalpha returns falsestring = ""print(string.isalpha());
In the code above:
-
We call
isalphaon thetextstring. Theisalphawill returnTruebecause all the characters of the string are alphabetic. -
We call
isalphaon theab3string. Theisalphawill returnFalsebecause the string contains a numerical character. -
We call
isalphaon theI have spacestring. Theisalphawill returnFalsebecause the string contains a space character. -
We call
isalphaon the empty string. For the empty string, theisalphamethod will returnFalse.