Syntax of Python isalpha()
string.isalpha()
Parameters of isalpha() in Python
The isalpha() function does not take any parameters. It operates on a string and checks if all characters in the string are alphabetic. If the string contains any non-alphabetic characters, the function returns False.
Return Value of isalpha() in Python
The isalpha() function returns a boolean value. It returns True if all characters in the string are alphabetic (letters from A-Z or a-z) and the string is not empty. If the string contains non-alphabetic characters like numbers, spaces, or special characters, or if it's empty, the function returns False.
Uses of isalpha() Function in Python
The isalpha python function can be helpful in many situations, especially when working with string data. Here are some examples of its uses:
-
Validating user input: If you're building an application that requires the user to input their name. Then we can use isalpha() to check if the information consists only of alphabetical characters.
-
Data cleaning: When working with text data, we may need to remove all non-alphabetic characters from a string. We can use isalpha() in combination with string manipulation functions such as replace() or regular expressions to achieve this.
-
Text processing: If we are working on a natural language processing (NLP) project, we may need to tokenize text into individual words. We can use isalpha() to check if a token consists only of alphabetical characters before adding it to a list of words.
-
Password validation: If we are building a password validation system, use isalpha python function to check if the password contains only alphabetic characters. It may be a requirement for some password policies.
- Data analysis: If we are analyzing text data, count the number of alphabetic characters in a string. We can use isalpha python function in combination with the count() method to achieve this.
Examples of isalpha() with Code
The isalpha python function have many applications. Let us understand some of them:
- To check whether a string consists only of alphabetic order:
Python
string1 = "WelcomeToCodingNinjas"
string2 = "Happy learning, Ninja"
string3 = "12345"
print(string1.isalpha())
print(string2.isalpha())
print(string3.isalpha())

You can also try this code with Online Python Compiler
Run Code
The output of the above code will be boolean values:
True
False
False
As you can see, the first string contains only alphabets, not any other special characters. Therefore the output is True. The second string contains only alphabets but is space-separated, unique characters. Therefore, the output is False. And the third string consists of integer values. Therefore, the output is False.
- To remove non-alphabetic characters from a string
Python
string4 = "Welcome To Coding Ninjas!!!"
alpha_string = "".join(char for char in string4 if char.isalpha())
print(alpha_string)

You can also try this code with Online Python Compiler
Run Code
The above code will remove all the non-alphabetic characters from the string including white spaces and exclamation marks.
WelcomeToCodingNinjas
-
To check if the string contains even a single alphabetic value
Python
string5 = "123d45"
string6 = "@#$%^&*()_+"
print(any(char.isalpha() for char in string5))
print(any(char.isalpha() for char in string6))

You can also try this code with Online Python Compiler
Run Code
In the above Code, the string5 consists of one alphabet; therefore, it will return True. The string6 does not contain any alphabet; consequently, it will return False.
True
False
-
To count the number of alphabetic characters in a string
Python
string = "Hell@ N!nja!!! Welc@me to C@ding N!nja"
count = sum(1 for c in string if c.isalpha())
print("The string contains {0} alphabetical characters". format(count))

You can also try this code with Online Python Compiler
Run Code
The above string contains 25 letters, hence the output will be:
The string contains 25 alphabetical characters
-
To check if the input consists of alphabets only
The outputs of above code will be:
Python
string = input("Entered string: ")
if string.isalpha():
print(string + " \nValid input")
else:
print(string + " \nInput contains non-alphabetical characters(white spaces) too.")

You can also try this code with Online Python Compiler
Run Code
If we entered a string without any spaces or special characters. Then our output will be like.
Entered string: Ninja
Valid Input
Errors and Exceptions in Python isalpha() Function
If an error or exception occurs while using the isalpha() function, it is usually due to one of the following reasons:
-
Non-String Input: The isalpha() function only works with strings. If we try to use it with any other data type (such as integers, floats, or lists), we will get a TypeError.
-
Special Characters: The isalpha() function only considers letters. If there are any special characters (such as punctuation marks or spaces) in the string, it will return False.
-
Empty Strings: If the input string is empty, the isalpha() function will always return False.
- Unicode Characters: The isalpha() function only works with ASCII characters. If we have any Unicode characters in our string (like accented letters or characters from non-Latin scripts), it may not work as expected.
You can also practice with the help of Online Python Compiler
Frequently Asked Questions
Does isalpha() consider whitespace as alphabetic characters in Python?
No, isalpha() does not consider whitespace as alphabetic characters. It only considers the 26 alphabetical characters (A-Z and a-z) as alphabetic characters.
What is the difference between isalpha() and isalnum() in Python?
isalpha() checks if all characters in a string are alphabetical characters only, while isalnum() checks if all characters in a string are either alphabetic or numeric. Therefore, isalnum() returns True if a string contains both alphabetical and numeric characters, while isalpha() returns False in this case.
Can isalpha() function be used with non-English alphabets?
Yes, isalpha() function can be used with non-English alphabets such as accented letters or letters with diacritics. It checks for any character that is considered a letter in the Unicode database.
Can isalpha() function be used on an empty string?
No, isalpha() function returns False if the string is empty (contains no characters). As the string is empty, we can’t perform any function on that. But the len() function can be used to check its length.
Conclusion
In this article, we focussed on isalpha() function in Python. We discussed its uses, examples and errors and exceptions of isalpha() in Python. To learn more about Python, Import, and Modules in Python, you can visit Code360. Refer to our guided paths to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the interview experiences and interview bundle for placement preparations.
Recommended Readings:
You can also consider our paid courses such as DSA in Python to give your career an edge over others!