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.
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.
Given below is the syntax of the
isAlphabetic = givenString.isalpha()
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
False according to the following logic.
Trueif all of the characters in the given string are only alphabets.
Falseif not all the characters in the given string are alphabets.
Given below are the two examples demonstrating the usage of the Python String
Example 1. Checking a given String contains all the alphabets or not using
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
#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())
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.")
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!