isalpha() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The isalpha() function is a built-in function used for string handling in python, which checks if the single input character is an alphabet or if all the characters in the input string are alphabets.

Introduction to isalpha()

While learning Python and working with strings, one might want to know or check if the string you are handling contains only alphabets and no special characters or numbers. Say you have a domain name validator, which has a strict rule of not allowing anything other than alphabets. That's where the isalpha() in python function comes in handy. We use this method to check if all the characters in a string are alphabets or not.

Syntax of isalpha() in Python

The syntax of the remove function in python is explained below:

The isalpha function is used with the string to checks if the single input character is an alphabet or if all the characters in the input string are alphabets. Read along to learn more about its parameter.

Parameters of isalpha() in Python

We can see that it doesn't accept a parameter as we have understood the syntax of the isalpha function in python.

Return Value of isalpha() in Python

isalpha() in python is a boolean function and hence has return values as either True or False.

The return value is : True when all the characters in the input string to the method islapha() in python are alphabet. False if 1 or more non-alphabetic characters are in the string given as input to the isalpha() method.

Errors and Exceptions

When do errors or exceptions occur while using this particular method in python?

  1. You know that the method isalpha() in python does not take any parameters, so, if you happen to pass any parameters, an error occurs. More specifically, a TypeError.

Output

  1. If the string consists of just uppercase, or just lowercase or even mixed case alphabets, the method isalpha() in python still returns True.

Output

Here, we have initialized different strings with different cases in this code. One string with all uppercase alphabets, one with all lowercase alphabets, while one with mixed case alphabets to show that the method isalpha() in python will still return true for all the above strings.

  1. While using the method isalpha() in python, you must keep in mind that spaces are not considered as alphabets; hence, adding a space to the string will result in the isalpha() method returning False.

In this piece of code, we can see that our string consists of space right after the word returns, and spaces are not considered alphabets; hence, the isalpha() method will return False.

Example 1: Working of isalpha()

Let's look at some more examples of the isalpha() in python method to clarify our understanding.

Output

As we can see in the first example, the string word also contains some numbers. The isalpha() method in python will return True only if all the characters in the string are alphabets. But since this string has numbers and alphabets, the method isalpha() in python returns False.

In the second example, the string only comprises numbers; hence the isalpha() method will return False.

Example 2: Practical Application

Counting alphabets in string using isalpha() in python

If you have a string that is a mix of alphabets, numerals, spaces, etc., and you need to know the total number of alphabetical characters in the string, then the method isalpha() in python can help with it.

How will we do it? Here's the algorithm we follow, we initialize a counter variable that will hold the current value of the number of alphabets encountered. It is initialized to 0. We then iterate over the complete string, element by element, and whenever an alphabet is encountered, we increment the count variable by 1.

Let's look at the code now.

Output:

What if you also wanted to extract the alphabet from the sentence? We create a new string, and every time we encounter an alphabet, we increase the counter by 1 and add the current character to the new string.

Output :

As discussed previously, spaces are not alphabets hence our new string does not contain the spaces.

Example 3:

The method isalpha() in python identifies unicode alphabets of other international languages.

For example, here we have a string in German:

Output:

The method isalpha() in python is not language-dependent, it works on languages other than English.

Example 4: Identify non-alphabets in a string

The method isalpha() in python can also be used to identify the non-alphabets in the string. How do we do that? If an element of the string is an alphabet, then we do not append it to the result string. However, if the isalpha() function returns False for a character, we can add it to our result string.

Output:

Conclusion

  • The method - isalpha() in python checks if the string contains only alphabets
  • It returns True if there are only alphabets, and False if not
  • Some errors and exceptions are - passing parameters(TypeError raised), string with both lower and upper cases, and adding spaces. Spaces result in isalpha() returning False.
  • Syntax = string.isalpha()
  • The method - isalpha() in python can be used for counting the number of alphabets or non-alphabets in a string. It can even identify Unicode alphabets of languages other than English.

See Also: