Python String isalpha() Method with Advanced Examples

What is Isalpha() method in Python?

The isalpha() method is used to know whether the characters in the string are only alphabets.

The word “alpha” in the isalpha() method means alphabet.

isalpha() is one of the Python string methods that contribute to distinguishing the alphabet characters that we use in our Python programs.

Learning this method will expand your knowledge of Python programming.

Syntax:

The syntax of the isalpha() method is displayed below.

string.isalpha()

This syntax retains the semantics(function) of the Python string isalpha() method.

isalpha() parameters

In Python, the isalpha() method for string doesn’t have any parameters.

Moreover, the isalpha method returns true if all characters in the Python string only contain alphabets such as:

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz

In contrast, the isalpha() method returns false if it is an empty string or it contains the following characters:

_`~!@#$%^&*()1234567890.,

Remember: Even if the string in Python has alphabets, the isalpha() method will return false when there’s a single different character other than the alphabet.

You can learn more about Python by checking out the Python tutorials.

How to use isalpha in Python?

The first set of examples will give you hints about how the isalpha() method in different scenarios.

Example 1: Simple name

name = "Jessica"
print(name.isalpha())

Output:

True

Example 2: Full name

name = "Jessica Sojo"
print(name.isalpha())

Output:

False

Example 3: Name with number

name = "[email protected]"
print(name.isalpha())

Output:

False

The second example explains how the isalpha() method works using an if-else statement.

Example 4: Using if-else

x = "JesicaSojo"

if x.isalpha() == True:
   print("The string only contain alphabets.")
else:
    print("Other characters were not alphabets.")

Output:

The characters only contain alphabets.

What is the difference between Isalnum and Isalpha?

Here are the distinctions between isalnum() and isalpha() methods:

The Python string isalpha() method is for alphabets, and the isalnum() is for both alphabet and numeric characters.

While the isalpha consists of “is” and “alpha”, meaning for alphabet, the isalnum consists of “is”, “al”, and “num”, meaning for alphabet and numbers.

Therefore, isalpha confirms if string characters are all alphabet, and isalnum confirms if the string characters only contain alphabets and numbers.

The syntax of islanum is:

string.isalnum()

This Python string isalnum() method is applicable for strings that contain alphanumeric characters.

It implies that the method will return true if the string contains alphabets (AaBbCc…Zz) and numbers (012…9); otherwise, it returns false.

The isalnum() method returns false when the string has the following:

_!@#$%^&*(),.

Set of examples using isalnum() method in Python.

Example 1:

name = "PythonForFree123"
print(name.isalpha())

Output:

True

Example 2:

name = "Python For Free 123"
print(name.isalpha())

Output:

False

How do you use Isalpha in a list in Python?

The application of the isalpha() method in the list will be the same as it applies in strings.

Since it is mentioned that the isalpha() is a built-in method of Python that handles strings, it can also handle lists.

However, its output will base on the overall list, which probably returns false because the Python list can include objects, symbols, and other lists aside from alphabets.

Summary

In summary, this tutorial complements the need to understand the isalpha() Python string method.

This method is as important as the other Python built-in methods because it is only applicable to letters in the alphabet.

Distinguishing the types of strings that you use in your Python program can improve your skills and boost your knowledge in programming.

Performing the isalpha() method can also help you experiment with your programs and add creativity to them.

Leave a Comment