Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

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

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring