Home »
Python
Python String isalnum() and isalpha() Methods
By IncludeHelp Last updated : December 15, 2024
String isalpha() Method
isalpha() method returns true if all characters of a string are only alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’). If there is any non alphabet character in the string, method returns false.
Syntax
string.isalpha()
Parameters
None
Return Type
- true - if all characters of the string are alphabets.
- false - if any of the character of the string is not an alphabet (like number, space, special characters).
Example
# str1 with only alphabets
str1 = "Helloworld"
# str2 with alphabets, space and special character
str2 = "Hello world!"
# str3 with alphabets, space and numbers
str3 = "Hello world 100"
# check whether string contains only alphabets or not
print(str1.isalpha())
print(str2.isalpha())
print(str3.isalpha())
Output
True
False
False
Python isalnum() Method
isalnum() method returns true if all characters of a string are either alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’) or numbers (0 to 9) or both, otherwise method returns false.
Syntax
string.isalnum()
Parameters
None
Return Type
- true - if all characters of the string are alphabets or/and numbers.
- false - if any of the character of the string is not either an alphabet or a number.
Example
# str1 with alphabets and numbers
str1 = "Helloworld123"
# str2 with only alphabets
str2 = "Helloworld"
# str3 with numbers only
str3 = "12345"
# str4 with alphanumeric characters and space
str4 = "Amit Shukla 21"
# check whether string contains
# only alphanumeric characters or not
print(str1.isalnum())
print(str2.isalnum())
print(str3.isalnum())
print(str4.isalnum())
Output
True
True
True
False