Python program to count upper and lower case characters without using inbuilt functions
Given a string that contains both upper and lower case characters in it. The task is to count number of upper and lower case characters in it without using in-built functions.
Counting the upper and lower case characters of a string can be easily done using isupper() and islower() functions, refer this. But doing the same without help of any inbuilt function is quite exciting. Let’s see how this can be done :
Examples :
Input : Introduction to Python Output : Lower Case characters : 18 Upper case characters : 2 Input : Welcome to GeeksforGeeks Output : Lower Case characters : 19 Upper case characters: 3
Below is the implementation of above idea :
# Python3 program to count upper and # lower case characters without using # inbuilt functions def upperlower(string): upper = 0 lower = 0 for i in range(len(string)): # For lower letters if (ord(string[i]) >= 97 and ord(string[i]) <= 122): lower += 1 # For upper letters elif (ord(string[i]) >= 65 and ord(string[i]) <= 90): upper += 1 print('Lower case characters = %s' %lower, 'Upper case characters = %s' %upper) # Driver Code string = 'GeeksforGeeks is a portal for Geeks'upperlower(string) |
Output :
('Lower case characters = 27', 'Upper case characters = 3')
Recommended Posts:
- Python regex to find sequences of one upper case letter followed by lower case letters
- isupper(), islower(), lower(), upper() in Python and their applications
- Python | Pandas Series.str.lower(), upper() and title()
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
- Python program to convert camel case string to snake case
- Lower and Upper Bound Theory
- Python | Toggle characters in words having same case
- Inbuilt Data Structures in Python
- Python | Permutation of a given string using inbuilt function
- Python | Count the Number of matching characters in a pair of string
- Python program to check if a string contains all unique characters
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Python | String upper()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



