isalpha() in Python

Vaibhav Vaibhav Apr 12, 2022
isalpha() in Python

A group of characters joined together to form a string. These characters can be anything; letters such as a, B, y, and Z, numbers such as 1, 0, 9, and 8, special characters such as !, &, * and %.

When working on real-world applications, developers have to validate strings to ensure that the data does not give birth to unexpected bugs. Validation includes cases such as checking for blocklisted characters, checking if the string is uppercase, or if it contains just numbers or not.

Since these tasks are pretty standard, almost all the programming languages own some utility. In this article, we will learn about one such in-built method, isalpha() in Python.

isalpha() Method in Python

The isalpha() method checks if a string is made up of just letters or not.

If it finds any other character, such as a number or a special character, it returns False. Otherwise, for a valid string, it returns True.

The isalpha() method can be called on any string.

Refer to the following Python code for some examples.

print("abcdefgh".isalpha())
print("qwerty123456".isalpha())
print("3333.3333".isalpha())
print("#&%^(*@)".isalpha())
print("AbcOSCgSjcHdksp".isalpha())

Output:

True
False
False
False
True

Following is the explanation for each string.

  1. True because it contains just letters.
  2. False because it contains numbers too.
  3. False because it contains numbers.
  4. False because it contains special characters.
  5. True because it contains just letters; it does not matter if they are lowercase or uppercase.
Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python String