Many times we need to check whether the string contains only letters or not. It is very useful and time-saving if we have some kind of in-built function available to check this problem. Talking about Python there is an inbuilt function available which is the python isalpha. To solve our problem in just two lines. The python isalpha() function is also very useful when we are taking the user input as only alphabets.
Python isalpha() technique is an integrated function and it checks for the existence of alphabets-only in a String. If all of the characters in the string are alphabets, it yields True. If the string includes other characters besides alphabets, for instance, a number or some special characters, it yields False. Alphabets from the English language include characters from A-Z and a-z.
Python isalpha() Syntax
Here the string refers to the string in question, i.e the string which is being checked for containing only alphabets or not.
Python isalpha() method does not take any parameters.
Return Type of isalpha()
The Python isalpha() method returns the Boolean value True if each character in a string is a letter; otherwise, it returns the Boolean value False. In Python, space isn’t an alphabetical character, so if a string includes a space, the method will return False.
Examples of Python isalpha()
Let’s walk through examples to demonstrate how this method works.
Example 1: Python isalpha()
Let’s say that we’re building a registration form for a scheduling program. To be able to sign up customers must submit their first name, surname, email address, and a password. Whenever someone inserts a name, we would like to check to be certain those names contain letters that they can be processed by our program properly.
We may use the isalpha() method to confirm that the name a user submits only includes letters. Here’s an example of a program that would perform this function:
first_name = input("Enteryour first name?") surname = input("Enter your last name?") print(first_name.isalpha()) print(surname.isalpha())
Enter your first name:Karan Enter your last name:Singh65 True False
Our code breaks. On the first two lines, we use the Python input() method to collect an individual’s first and surnames. Then we use the Python isalpha() method to check whether these names only contain alphabetical characters. When our program evaluates first_name.isalpha(), it yields True since the value our program stored as first_name contains only letters.
But when our program evaluates the surname, it yields False since our user added a number as their last name.
Example 2: When String Contains Whitespace
name = "Karan Singh" print(name.isalpha())
Explanation: In the above example we get False as the output because there is space between the string Karan Singh. And in Python space is not considered as Alphabet so we get False as the output.
Example 3: Using if-else Statement to Check Whether a String Contains All Alphabet or Not
name = "PythonPool" if name.isalpha() == True: print("All characters of Sting are alphabets") else: print("All characters of String are not alphabets.")
All characters of Sting are alphabets
So in the above example, we checked whether the string contains all alphabets or not. We use the conditional statements if and else to check this.
Example 4: Count the number of alphabets in a string in Python
As isalpha() method returns True if the given string contains alphabets. By applying this process to every element of this string using a loop we can count the number of characters of a string. We increment a count variable to count the number of characters in a given string.
Let us take a peek over the code:
str="PythonPool is the best Website to learn python 100%" count=0 for i in str: if(i.isalpha()): count=count+1 print("Number of Characters =",count)
Number of Characters = 39
In the above example, we’ve iterated during the string by every character. We’ve implemented isalpha() Strategy for every single character in the given string. If the isalpha() method returns True then the value of count will be incremented by 1 that counts the number of alphabets in a given string.
Point to be Noted for Using Python isalpha
Whitespaces, numbers and special characters are not considered as alphabets.
Errors and Exceptions
- It comprises no arguments; thus, an error occurs if a parameter is passed.
- Both uppercase and lowercase alphabets return “True”.
- Space isn’t thought of as the alphabet; thus, it returns”False”.
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python in 2020
So in this article, we have learned in detail about the isalpha Python Function. We learned all its examples and how it works in different circumstances.
Try to run the programs on your side and let me know if you have any queries.