Python Different Counts in String – Python program to count uppercase letters, lower case letters, vowels, consonants and spaces in the given string.
The Python program provides many conts of different types in a string. For example it will display the number of
- Uppercase letters
- Lowercase letters
- and Spaces
Output: Here is some sample output of this program run.
Enter a String: Hello World
The given string contains…
2 Uppercase letters
8 Lowercase letters
Explanation of key parts of the program:
- A series of variables (
spaces) are initialized to store the counts of different character types.
input()function is used to take user input for the string that needs to be analyzed.
forloop iterates through each character (
ch) in the input string.
- Inside the loop:
isupper()method is used to check if the current character is in uppercase, and if so, the
uppercasecounter is incremented.
islower()method is used to check if the current character is in lowercase, and if so, the
lowercasecounter is incremented.
- A tuple containing the lowercase and uppercase vowels is used to check if the character is a vowel. If it is, the
vowelscounter is incremented.
isalpha()method is used to check if the character is an alphabetic character (i.e., not a digit or special character), and if so, the
consonantscounter is incremented.
isspace()method is used to check if the character is a space, and if so, the
spacescounter is incremented.
- After the loop completes, the program prints the counts of each type of character based on the gathered data.
The program’s purpose is to provide insights into the composition of characters within the input string, helping the user understand the distribution of uppercase letters, lowercase letters, vowels, consonants, and spaces.
- Python Program to convert string in Sentence case
- Python Program to convert string to Opposite case
- Python Program to Convert String to Title Case
- Python Program to convert a string to lower case
- Python Program to Convert String to Upper Case
- Python Program To Show Calendar of a Year
- Python Program to output Calendar for a month
- Palindrome Program in Python
- Python String Program To Count Vowels
- Python Text File Program To Count Vowels
- Python File Program Count Characters in Text File
- Python Exception Handling FileNotFoundError
- Python Text File Read and Show File Contents
- Python file program read numbers write squares