Code to Count Alphabets, Numeric, Special character and Space in a string in Python
Code to Count Alphabets, Numeric, Special character and Space in a string
In this article, we will discuss the concept of the Code to Count Alphabets, Numeric, Special character and Space in a string
In this post, we are going to learn how to count Alphabets, Numeric, Special character and space of the given String in Python programming language
Count Alphabets, Numeric, Special character and Space in Python
Count Alphabets, Numeric, Special character and Space using for loop
The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space of the given string using for loop in Python programing language
Program 1
#Python code to Count Alphabets, Special character Numeric values and space str=input("Please enter a string: ")#take input from the user alphabets,num,special,space=0,0,0,0;#variable declaration and initilization for i in range(len(str)): if(str[i]>='A' and str[i]<='Z') or (str[i]>='a' and str[i]<='z'): #check Alphabets letters alphabets+=1 elif(str[i]>='0' and str[i]<='9'):#check numeric value num+=1 elif(str[i]==' '):#check space space+=1 else: special+=1 print("Alpabets letters: ",alphabets) print("\nnumbers: ",num) print("\nSpace: ",space) print("\nSpecial characters: ",special)
When the above code is executed, it produces the following result
Please enter a string: Your code:code4coding.com Alphabet letters: 21 numbers: 1 Space: 1 Special characters: 2
Approach
- Declare a String variable as str;
- Declare and initialize integer variables as int Alphabets, num,space, Special=0,0,0,0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A for-loop is used to count every total of the given string.
- Use an if condition to test if(str[i]>=’A’ and str[i]<=’Z’) or (str[i]>=’a’ and str[i]<=’z’): If it is true, The Alphabets becomes Alphabets + 1(Alphabets=Alphabets+1);
- When it is false, control moves to the elif part and evaluates the test-expression of elif. if it is true The num becomes num+1(num=num+1);
- When it is false, control moves to the next elif part and evaluates the test-expression of elif. if it is true The space becomes space+1(space=space+1);
- When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
- Finally, the program displays the total number of the Alphabets, Numeric, Special character and Space of the given string
Count Alphabets, Numeric, Special character and Space using Function
The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space of the given string using the built-in function in Python programing language
Program 2
#Python code to Count Alphabets, Special character Numeric values and space str=input("Please enter a string: ")#take input from the user alphabets,num,special,space=0,0,0,0;#variable declaration and initilization for i in range(len(str)): if(str[i].isalpha()): #check Alphabets letters alphabets+=1 elif(str[i].isdigit()):#check numeric value num+=1 elif(str[i].isspace()):#check space space+=1 else: special+=1 print("Alpabets letters: ",alphabets) print("\nnumbers: ",num) print("\nSpace: ",space) print("\nSpecial characters: ",special)
When the above code is executed, it produces the following result
Please enter a string: code4coding.com:For learners Alphabets letters: 24 numbers: 1 Space: 1 Special characters: 2
Suggested for you
Similar post
C++ code to count the total number of characters in the given string
C code to count the total number of characters in the given string
Python code to count the total number of characters in the given string
Java program to count the total number of characters in the given string including space
C program to count the total number of characters in the given string including space
C++ program to count the total number of characters in the given string including space