{"id":16529,"date":"2021-08-12T09:37:41","date_gmt":"2021-08-12T04:07:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=16529"},"modified":"2021-11-22T18:38:25","modified_gmt":"2021-11-22T13:08:25","slug":"python-program-to-count-the-number-of-alphabets-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-number-of-alphabets-in-a-string\/","title":{"rendered":"Python Program to Count the Number of Alphabets in a String"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count the Number of Null elements in a List<\/a>.<\/p>\n Given a string, the task is to count the Number of Alphabets in a given String.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Count the Number of Alphabets in a String.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\nisalpha() Method<\/strong>:<\/h4>\n
Given String = hello btechgeeks<\/pre>\n
The Number of Characters in a given string { hello btechgeeks } = 15<\/pre>\n
Given String = good morning btechgeeks<\/pre>\n
The Number of Characters in a given string { good morning btechgeeks } = 21<\/pre>\n
Program to Count the Number of Alphabets in a String<\/h2>\n
\n
Method #1: Using isalpha() Method (Static input)<\/h3>\n
\n
# Give the String as static input and store it in a variable.\r\ngvn_str = \"hello btechgeeks\"\r\n# Take a variable say 'count' and initialize it's value with '0'\r\ncount_no = 0\r\n# Loop from 0 to the length of the above given String using For Loop.\r\nfor itrtor in gvn_str:\r\n # Inside the loop, check whether\u00a0 if the value of iterator is alphabet or\r\n # using built-in isalpha() method inside the if conditional statement.\r\n if(itrtor.isalpha()):\r\n # If the given condition is true ,then increment the above initialized count value by '1'.\r\n \tcount_no = count_no+1\r\n# Print the number of Alphabets in a given string by printing the above count value.\r\nprint(\r\n \"The Number of Characters in a given string {\", gvn_str, \"} = \", count_no)\r\n<\/pre>\n
The Number of Characters in a given string { hello btechgeeks } = 15<\/pre>\n
Method #2: Using isalpha() Method (User input)<\/h3>\n
\n
# Give the String as user input using the input()function and store it in the variable.\r\ngvn_str = input(\"Enter some random String = \")\r\n# Take a variable say 'count' and initialize it's value with '0'\r\ncount_no = 0\r\n# Loop from 0 to the length of the above given String using For Loop.\r\nfor itrtor in gvn_str:\r\n # Inside the loop, check whether\u00a0 if the value of iterator is alphabet or\r\n # using built-in isalpha() method inside the if conditional statement.\r\n if(itrtor.isalpha()):\r\n # If the given condition is true ,then increment the above initialized count value by '1'.\r\n \tcount_no = count_no+1\r\n# Print the number of Alphabets in a given string by printing the above count value.\r\nprint(\r\n \"The Number of Characters in a given string {\", gvn_str, \"} = \", count_no)\r\n<\/pre>\n
Enter some random String = good morning btechgeeks\r\nThe Number of Characters in a given string { good morning btechgeeks } = 21<\/pre>\n
\n