Python String isalpha() Method
The str.isalpha()
method returns True
if all the characters in the string are alphabets (both uppercase and lowercase); otherwise, it returns False
.
Example:
text = "PythonTutorial"
print(text.isalpha())
Output
True
Syntax
The Syntax of the str.isalpha()
function is:
str.isalpha()
Here, str
is a string.
Parameters
The str.isalpha()
function does not take any parameters.
Return Value
The str.isalpha()
function returns:
True
- if all the characters in the string are alphabets.False
- if at least one of the characters in the string is non-alphabet or an empty string,
Example 1: Python Program to check if the string is an alphabet
The isalpha()
method returns True
only if all the characters in the string are alphabets(Uppercase/Lowercase); if it finds other than alphabets such as space, numbers, special characters in the string, it returns False
.
# string with only alphabets
text = "Python"
print(text.isalpha())
# string with alphabets and space
text = "Python is Awesome"
print(text.isalpha())
# string with alphabets and number
text = "Python3"
print(text.isalpha())
Output
True
False
False
Example 2: How to use the isalpha() method in a program?
The below program will iterate all the elements in the list, and using isalpha()
method, it determines whether the element/name is an alphabet or not.
# program to check if the first name has only alphabets
fname_lst = ["Chandler", "Rachel", "Ross", "Monica", "Joey5", "Phoebe Buffay"]
for name in fname_lst:
if (name.isalpha() == False):
print(f"Not all the characters in the name {name} are alphabets")
Output
Not all the characters in the name Joey5 are alphabets
Not all the characters in the name Phoebe Buffay are alphabets
Reference: Python Official Docs