Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python program to count upper and lower case characters without using inbuilt functions

  • Difficulty Level : Basic
  • Last Updated : 24 Nov, 2020

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 :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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'
Lower case characters = 27 Upper case characters = 3

Alternative Method:-

s = "The Geek King"
l,u = 0,0
for i in s:
    if (i>='a'and i<='z'):
        # counting lower case
    if (i>='A'and i<='Z'):
        #counting upper case
print('Lower case characters: ',l)
print('Upper case characters: ',u)
Lower case characters:  8
Upper case characters:  3

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!