Python string isalpha() Method will quickly check if a string is made up of only alphabetical characters, and won’t include any numbers and special characters like underscores or dashes. This is a great function to use when you need to ensure that a string is safe for use in an application.
In this article, we’ll show you how to use the Python string isalpha() method to check for alpha characters in a string. So let’s get started.
Table of Contents
What is String Isalpha( ) Method?
To determine if the single input character is an alphabet or if all the characters in the input string are alphabets, use the isalpha function with the string. If all of the string’s characters are alphabets, this method returns True or else false.
Working of Isalpha() Method in String
In Python, isalpha() function can determine whether a character is an alphabetic letter. This function takes in a single argument (which must be a string) and returns True if the string contains alphabetical letters and False otherwise.
String Contains Alphabets
string = "WelcomeToCodeleaks" print(string.isalpha())
String Contains Spaces
string = "Welcome To Codeleaks" print(string.isalpha())
String Contains Numbers
string = "Welcome To Codeleaks 100" print(string.isalpha())
Error and Exception
There seems to be an error or exception when calling the string.alpha() function. following are the errors and exceptions that may occur:
- You know that passing any parameters to the python function isalpha() leads to an error. A TypeError is a more specific error.
str = "codeleaks" print(str.isalpha()) print(str.alpha(0))
- The method isalpha() in python returns True if the string is only uppercase, lowercase, or even mixed case alphabets.
string = "ABCD" string1 = "abcd" string2 = "ABcd" print(string.isalpha()) print(string1.isalpha()) print(string2.isalpha())
- Spaces are not regarded as alphabets when using the isalpha() python method; therefore, adding a space to the string will cause the isalpha() function to return False.
string = "welcome to codeleaks" print(string.isalpha())
Counting Alphabets in String using Isalpha() Method
The method isalpha() in Python may be used to determine the total number of alphabetical letters in a string if it contains a mix of alphabets, numbers, spaces, and other characters.
string = "Welcome to codeleaks" count = 0 for char in string: if char.isalpha(): count += 1 print("total number of characters are:", count)
Identifying Unicode Alphabets using Isalpha() Method
The method isalpha() in Python recognizes unicode alphabets from other foreign languages, and it works on languages other than English.
string = 'caractères' print(string.isalpha())
Identifying Non-Alphabets in Character using Isaplha() Method
The non-alphabets in the string may also be identified using the method isalpha() in Python. So, how do we achieve that? We do not add an alphabet to the result string if it is part of the string. We can, however, add it to our result string if the isalpha() function returns False for a character.
def nonAlphabets(string: str) -> str: res = "" for char in string: if not char.isalpha(): res += char return res string = "print nonAplha character 5$" print(nonAlphabets(string))
What is the real life implementation of python?
We can use this function in any registration form where the name field only requires alpha characters.
Is alpha() is an built-in function?
The isalpha() function is a built-in function used for string processing in python. The single input character is checked to be an alphabet, or all the characters in the input string are alphabets.
In this article, you have learned how Python String isalpha() method can be used whether a string has alpha or not. You also saw some of its uses, such as determining whether your string contains only alphabets or any other characters that are not alphabets. Embrace Python coding and start using these helpful tools. Thanks for Reading.