What is the String.isalpha() method in Python?

The isalpha() method of the String class in Python is a built-in method that checks if a specified argument includes only alphabets (A-Z and/or a-z).

Syntax

string.isalpha()

Parameters

The isalpha() method takes no parameters.

Return type

isalpha() method returns true if all characters in the string are alphabets. Otherwise, it returns false.

The figure below shows the different return types of the isalpha() method for different arguments:

string = 'Maria'
string.isalpha() #returns true
   
string = 'Maria04'
string.isalpha() #returns false

string = 'Maria Elijah'
string.isalpha() #returns false

Code

The following program shows how to use the isalpha() method.

# using isalpha()
# Given string
string='_Maria'
print(string.isalpha())
string1 = 'Maria Elijah'
count=0
# Iterate the string and check for alphabets
# Increment the counter if an alphabet is found
# Finally print the result
for x in string1:
if (x.isalpha()) == True:
count+=1
print(f"The string contains {count} alphabet")

Explanation

The code above first uses the isalpha() method on the string variable. Next, the code iterates over each character in a string, and uses the isalpha() method to check if the current character is an alphabet or not.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources