Python String isalpha () and its application

Counters | Python Methods and Functions | String Variables

In Python, isalpha () — it is a built-in method used to process strings. 
The isalpha () methods return True if all characters in the string are alphabets. Otherwise, it returns "False". 
This function is used to check if the argument contains alphabet 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 

# Python code to implement isalpha ()

 
# checking alphabets

string = 'Ayush'

print (string.isalpha ())

 

 

string = 'Ayush0212'

print (string.isalpha ())

  
# check if space is alphabetical

string = 'Ayush Saxena'

print (string.isalpha ())

Output:

 True False False 

Errors and exceptions

  1. It contains no arguments, so an error occurs when passing the parameter
  2. Both uppercase and lowercase alphabets return "True"
  3. Space is not considered an alphabet, so it returns "False"

Usage: If given a string in python, count the number of alphabets on a line and print the alphabets. 
Examples:

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

Algorithm

1. Initialize a new line and variable count to 0.
2. Go through the given string character by character to its length, check if the character is an alphabet. 
3. If it's an alphabet, increment the counter by 1 and add it on a new line, otherwise skip to the next character. 
4. Print the counter value and a new line.

# Python program for illustration
# counting the number of alphabets
# using isalpha ()

  
# Specified string

string = 'Ayush Saxena'

count = 0

 
# Initialize new lines

newstring1 = ""

newstring2 = ""

  
# Iterate a string and check alphabets
# Increment counter if alphabet found
# Finally, print invoice

for a in string:

if (a.isalpha ()) = = True :

count + = 1

newstring1 + = a

pr int (count)

print (newstring1 )

 
# This line

string = 'Ayush0212'

count = 0

for a in string:

if (a.isalpha ()) = = True :

count + = 1

  newstring2 + = a

print (count)

print (newstring2)

Output:

 11 AyushSaxena 5 Ayush