Python String isalpha() and its application

In Python, isalpha() is a built-in method used for string handling.
The isalpha() methods returns “True” if all characters in the string are alphabets, Otherwise, It returns “False”.
This function is used to check if the argument contains any alphabets characters such as :

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Syntax :

string.isalpha()
Parameters:
isalpha() does not take any parameters
Returns :
1.True- If all characters in the string are alphabet.
2.False- If the string contains 1 or more non-alphabets.

Examples:



Input : string = 'Ayush'
Output : True

Input : string = 'Ayush Saxena'
Output : False

Input : string = 'Ayush0212'
Output : False
filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code for implementation of isalpha()
   
# checking for alphabets
string = 'Ayush'
print(string.isalpha())
   
  
string = 'Ayush0212'
print(string.isalpha())
   
# checking if space is an alphabet
string = 'Ayush Saxena'
print( string.isalpha())

chevron_right


Output:

True
False
False

Errors and Exceptions

  1. It contains no arguments , therefore an error occurs if a parameter is passed
  2. Both uppercase and lowercase alphabets return “True”
  3. Space is not considered to be alphabet, therefore it returns “False”

Application : Given a string in python, count number of alphabets in the string and print the alphabets.
Examples:

Input : string = 'Ayush Saxena'
Output : 11
         AyushSaxena

Input : string = 'Ayush0212'
Output : 5
         Ayush

Algorithm

1. Initialise a new string and variable counter to 0.
2. Traverse the given string character by character upto its length, check if character is an alphabet.
3. If it is an alphabet, increment the counter by 1 and add it to a the new string, else traverse to the next character.
4. Print the value of the counter and the new string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate
# counting number of alphabets 
# using isalpha()
  
# Given string
string='Ayush Saxena'
count=0
  
# Initialising new strings
newstring1 =""
newstring2 =""
  
# Iterating the string and checking for alphabets
# Incrementing the counter if an alphabet is found
# Finally printing the count
for a in string:
    if (a.isalpha()) == True:
        count+=1
        newstring1+=a
print(count)
print(newstring1)
  
#Given string
string='Ayush0212'
count=0
for a in string:
    if (a.isalpha()) == True:
        count+=1
        newstring2+=a
print(count)
print(newstring2)

chevron_right


Output:

11
AyushSaxena
5
Ayush


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.