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) |
Lower case characters = 27 Upper case characters = 3
Alternative Method:-
s = "The Geek King"l,u = 0,0for i in s: if (i>='a'and i<='z'): l=l+1 #counting lower case if (i>='A'and i<='Z'): u=u+1 #counting upper case print('Lower case characters: ',l) print('Upper case characters: ',u) |
Lower case characters: 8 Upper case characters: 3
Recommended Posts:
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python - Extract Upper Case Characters
- 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)
- Lower and Upper Bound Theory
- Histogram Plotting and stretching in Python (without using inbuilt function)
- Arcade inbuilt functions to draw point(s) in Python3
- Arcade inbuilt functions to draw polygon in Python3
- Pandas - Convert the first and last character of each word to upper case in a series
- Python program to convert camel case string to snake case
- Python - Convert Snake case to Pascal case
- Python - Convert Snake Case String to Camel Case
- Python | Permutation of a given string using inbuilt function
- MoviePy â Displaying a Frame of Video Clip using inbuilt display method
- Inbuilt Data Structures in Python
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Python | Toggle characters in words having same case
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.
Improved By : Divyu_Pandey

