Contents
Python String isalpha() method
Python String isalpha() method is used to check if all characters in the string are alphabets and there is at least one character in the string.
Consider that given string is in x.
x = "HelloWorld"
Then the return value of x.isalpha() is
True
In this tutorial, you will learn the syntax and usage of String isalpha() method in Python language.
Syntax of isalpha() method
The syntax of String isalpha() method in Python is given below.
str.isalpha()
Parameters
The string isalpha() method takes no parameters.
Return value
The string isalpha() method returns a boolean value of True if all characters in the string are alphabets and there is at least one character in the string, otherwise False.
Examples
1. Checking if given string is alphabetic in Python
In this example, we take a string value in variable x. We have to check if all characters in the string are alphabets.
Call isalpha() method on the string object x and use the returned value as a condition in Python if else statement as shown in the following program.
Since all characters in the string are alphabets, isalpha() method returns True, and the if-block executes.
Python Program
x = "HelloWorld"
if x.isalnum():
print('Given string is ALPHABETIC.')
else:
print('Given string is NOT ALPHABETIC.')
Run Code CopyOutput
Given string is ALPHABETIC.
2. Checking if string with special symbols is alphabetic in Python
In the following program, we take a string value in variable x such that some of the characters in the string are special symbols like @, $, etc.
Since, not all characters in the string are alphabetic, isalpha() method returns False, and the else-block executes.
Python Program
x = "Hello@World"
if x.isalnum():
print('Given string is ALPHABETIC.')
else:
print('Given string is NOT ALPHABETIC.')
Run Code CopyOutput
Given string is NOT ALPHABETIC.
3. Checking if string with whitespaces is alphabetic in Python
In the following program, we take a string value in variable x such that some of the characters in the string are whitespace characters like single space, tab, new line, etc.
Since, not all characters in the string are alphabetic, isalpha() method returns False, and the else-block executes.
Python Program
x = "Hello World\n"
if x.isalnum():
print('Given string is ALPHABETIC.')
else:
print('Given string is NOT ALPHABETIC.')
Run Code CopyOutput
Given string is NOT ALPHABETIC.
4. Checking if an empty string is alphabetic in Python
In the following program, we take an empty string value in variable x, and check if this empty string is alphabetic.
Since, there is not at least one character in the string, isalpha() method returns False, and the else-block executes.
Python Program
x = ""
if x.isalnum():
print('Given string is ALPHABETIC.')
else:
print('Given string is NOT ALPHABETIC.')
Run Code CopyOutput
Given string is NOT ALPHABETIC.
Summary
In this tutorial of Python String Methods, we learned about String isalpha() method, its syntax, and examples.