In the previous article, we have discussed Python Program to Count the Number of Null elements in a List.
Given a string, the task is to count the Number of Alphabets in a given String.
isalpha() Method:
The isalpha() method is a built-in method for string-type objects. If all of the characters are alphabets from a to z, the isalpha() method returns true otherwise, it returns False.
- Python Program to Accept a Sentence and Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop
- Python Program to Implement the Latin Alphabet Cipher
- Python Program to Check if a Substring is Present in a Given String
Examples:
Example1:
Input:
Given String = hello btechgeeks
Output:
The Number of Characters in a given string { hello btechgeeks } = 15
Example 2:
Input:
Given String = good morning btechgeeks
Output:
The Number of Characters in a given string { good morning btechgeeks } = 21
Program to Count the Number of Alphabets in a String
Below are the ways to Count the Number of Alphabets in a String.
Method #1: Using isalpha() Method (Static input)
Approach:
- Give the String as static input and store it in a variable.
- Take a variable to say ‘count’ and initialize its value with ‘0’
- Loop from 0 to the length of the above-given String using For Loop.
- Inside the loop, check whether if the value of the iterator is an alphabet or using the built-in isalpha() method inside the if conditional statement.
- If the given condition is true, then increment the above-initialized count value by ‘1’.
- Print the number of Alphabets in a given string by printing the above count value.
- The Exit of the program.
Below is the implementation:
# Give the String as static input and store it in a variable. gvn_str = "hello btechgeeks" # Take a variable say 'count' and initialize it's value with '0' count_no = 0 # Loop from 0 to the length of the above given String using For Loop. for itrtor in gvn_str: # Inside the loop, check whether if the value of iterator is alphabet or # using built-in isalpha() method inside the if conditional statement. if(itrtor.isalpha()): # If the given condition is true ,then increment the above initialized count value by '1'. count_no = count_no+1 # Print the number of Alphabets in a given string by printing the above count value. print( "The Number of Characters in a given string {", gvn_str, "} = ", count_no)
Output:
The Number of Characters in a given string { hello btechgeeks } = 15
Method #2: Using isalpha() Method (User input)
Approach:
- Give the String as user input using the input() function and store it in the variable.
- Take a variable to say ‘count’ and initialize its value with ‘0’
- Loop from 0 to the length of the above-given String using For Loop.
- Inside the loop, check whether if the value of the iterator is an alphabet or using the built-in isalpha() method inside the if conditional statement.
- If the given condition is true, then increment the above-initialized count value by ‘1’.
- Print the number of Alphabets in a given string by printing the above count value.
- The Exit of the program.
Below is the implementation:
# Give the String as user input using the input()function and store it in the variable. gvn_str = input("Enter some random String = ") # Take a variable say 'count' and initialize it's value with '0' count_no = 0 # Loop from 0 to the length of the above given String using For Loop. for itrtor in gvn_str: # Inside the loop, check whether if the value of iterator is alphabet or # using built-in isalpha() method inside the if conditional statement. if(itrtor.isalpha()): # If the given condition is true ,then increment the above initialized count value by '1'. count_no = count_no+1 # Print the number of Alphabets in a given string by printing the above count value. print( "The Number of Characters in a given string {", gvn_str, "} = ", count_no)
Output:
Enter some random String = good morning btechgeeks The Number of Characters in a given string { good morning btechgeeks } = 21
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.