Python String isalpha() Method

Python String Isalpha() Method

This is a tutorial of the Python String isalpha() method. Learn to check if a given string only contains alphabetical characters or not with examples.

Python String isalpha()

The str.isalpha() method returns True if the given string contains only alphabetical characters (A-Z, a-z) and if not it returns False.

Note. By alphabets, we mean both the Uppercase and the Lowercase letters.

Syntax

Given below is the syntax of the str.isalpha() method.

isalpha() Parameters

This method does not take any arguments as it only checks the characters in the string on which it is applied, so no additional data is required in the form of arguments.

isalpha() Return Value

It returns the boolean value True or False according to the following logic.

  • Returns True if all of the characters in the given string are only alphabets.
  • Returns False if not all the characters in the given string are alphabets.

Examples

Given below are the two examples demonstrating the usage of the Python String isalpha() method.

Example 1. Checking a given String contains all the alphabets or not using str.isalpha() method

In this example, we’re applying the method isalpha() on five different strings to check the result, for what strings it returns True and for what it returns False.

#String with only alphabets
line = "HelloWTMatter"
print(line.isalpha())

#String with alphabets and a whitespace
line = "Hello WTMatter"
print(line.isalpha())

#String with alphabets and special symbols
line = "Hello!WTMatter?"
print(line.isalpha())

#String with Alphanumeric Characters
line = "Hello12345"
print(line.isalpha())

#String with only Numbers
line = "12345"
print(line.isalpha())

Output.

True
False
False
False
False

Example 2. Using the Python String isalpha() method with if-else statement

In the following example, we’ve directly used the str.isalpha() method as the condition for the if-else statement to print a string accordingly after checking if the given string only contains alphabets or not.

#Using str.isalpha() with if-else statement

#Given String
name = "HelloWorld12345"

if name.isalpha():
    print("name only contains alphabets.")
else:
    print("name also contains characters other than alphabets.")

Output.

name also contains characters other than alphabets.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *