Given a String. The task is to return the number of characters in the string.
Examples:
Input : test_str = ‘neveropen !!$*** best 4 all Geeks 10-0’
Output : 25
Explanation : Only alphabets, when counted are 25Input : test_str = ‘neveropen !!$*** best for all Geeks 10—0’
Output : 27
Explanation : Only alphabets, when counted are 27
Method #1 : Using isalpha() + len()
In this approach, we check for each character to be alphabet using isalpha() and len() is used to get the length of the list of alphabets to get count.
Python3
# Python3 code to demonstrate working of # Alphabets Frequency in String # Using isalpha() + len() # initializing string test_str = 'neveropen !!$ is best 4 all Geeks 10-0' # printing original string print ( "The original string is : " + str (test_str)) # isalpha() to computation of Alphabets res = len ([ele for ele in test_str if ele.isalpha()]) # printing result print ( "Count of Alphabets : " + str (res)) |
The original string is : neveropen !!$ is best 4 all Geeks 10-0 Count of Alphabets : 27
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using ascii_uppercase() + ascii_lowercase() + len()
In this, we perform the task of getting alphabets as a combination of upper and lowercase, using inbuilt functions, len() returns frequency.
Python3
# Python3 code to demonstrate working of # Alphabets Frequency in String # Using ascii_uppercase() + ascii_lowercase() + len() import string # initializing string test_str = 'neveropen !!$ is best 4 all Geeks 10-0' # printing original string print ( "The original string is : " + str (test_str)) # ascii_lowercase and ascii_uppercase # to check for Alphabets res = len ([ele for ele in test_str if ele in string.ascii_uppercase or ele in string.ascii_lowercase]) # printing result print ( "Count of Alphabets : " + str (res)) |
The original string is : neveropen !!$ is best 4 all Geeks 10-0 Count of Alphabets : 27
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using the sum() function
Use the sum() function along with a generator expression that checks if each character in the string is an alphabet using isalpha().
Python3
# Python3 code to demonstrate working of # Alphabets Frequency in String # Using sum() + isalpha() # initializing string test_str = 'neveropen !!$ is best 4 all Geeks 10-0' # printing original string print ( "The original string is : " + str (test_str)) # Using sum() and isalpha() to computation of Alphabets count res = sum ( 1 for c in test_str if c.isalpha()) # printing result print ( "Count of Alphabets : " + str (res)) |
The original string is : neveropen !!$ is best 4 all Geeks 10-0 Count of Alphabets : 27
Time Complexity: O(n)
Auxiliary Space: O(1)
METHOD 4:Using re module .
APPROACH:
This Python program counts the number of alphabets in a given string using the re.findall() function and regular expression.
ALGORITHM:
1.Import the re module.
2.Define a string variable containing the input string.
3.Use re.findall() function with the regular expression pattern ‘[a-zA-Z]’ to extract all the alphabets from the string.
4.Count the number of extracted alphabets using the len() function.
5.Print the count of alphabets.
Python3
# Python program to count the number of characters in a string import re # Given input str_input = "neveropen !!$ is best 4 all Geeks 10-0" # Counting alphabets in the string using regex and re.findall() function count = len (re.findall( '[a-zA-Z]' , str_input)) # Printing the count of alphabets in the string print ( "Count of Alphabets: " , count) |
Count of Alphabets: 27
Time complexity:
The re.findall() function has a time complexity of O(n), where n is the length of the input string. The len() function has a time complexity of O(1). Therefore, the overall time complexity of the program is O(n).
Auxiliary Space:
The space complexity of this program is O(n), where n is the length of the input string. This is because the re.findall() function extracts all the alphabets from the string and stores them in memory, which requires space proportional to the length of the input string. The space used by the len() function and the other variables is constant and can be ignored.