Open In App

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

Last Updated : 28 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string that contains both upper and lower case characters in it. The task is to count a number of upper and lower case characters in it.

 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

Method 1: Using the built-in methods

Python3




Str="GeeksForGeeks"
lower=0
upper=0
for i in Str:
      if(i.islower()):
            lower+=1
      else:
            upper+=1
print("The number of lowercase characters is:",lower)
print("The number of uppercase characters is:",upper)


Output

The number of lowercase characters is: 10
The number of uppercase characters is: 3

Time Complexity: O(n)
Auxiliary Space: O(1)

Explanation:

Here we are simply using the built-in method islower() and checking for lower case characters and counting them and in the else condition we are counting the number of upper case characters provided that the string only consists of alphabets.

 Method 2: Using the ascii values, Naive Method

Python3




# 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

Time Complexity: O(n)
Auxiliary Space: O(1)

Explanation:

Here we are using the ord() method to get the ascii value of that particular character and then calculating it in the particular range.

Method 3: Calculating the characters within the given range of ascii code

Python3




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


Output

Lower case characters:  8
Upper case characters:  3

Time Complexity: O(n)
Auxiliary Space: O(1)

Explanation:

Here we are iterating through the string and calculating upper and lower case characters using the ascii code range.

Method 4: Using ‘in’ keyword

Python3




# Python3 program to count upper and
# lower case characters without using
# inbuilt functions
string = 'GeeksforGeeks is a portal for Geeks'
upper = 0
lower = 0
up="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lo="abcdefghijklmnopqrstuvwxyz"
for i in string:
    if i in up:
        upper+=1
    elif i in lo:
        lower+=1
print('Lower case characters = %s' %lower)
print('Upper case characters = %s' %upper)


Output

Lower case characters = 27
Upper case characters = 3

Time Complexity: O(n)
Auxiliary Space: O(1)

Explanation:

Here we have taken all the upper and lower case characters in separate strings and then count how many characters are present in individual strings.

Method #5 : Using operator.countOf() method

Python3




import operator as op
Str = "GeeksForGeeks"
lower = "abcdefghijklmnopqrstuvwxyz"
l = 0
u = 0
for i in Str:
    if op.countOf(lower, i) > 0:
        l += 1
    else:
        u += 1
print("The number of lowercase characters is:", l)
print("The number of uppercase characters is:", u)


Output

The number of lowercase characters is: 10
The number of uppercase characters is: 3

Time Complexity: O(n)

Space Complexity: O(1)



Similar Reads

Python regex to find sequences of one upper case letter followed by lower case letters
Write a Python Program to find sequences of one upper case letter followed by lower case letters. If found, print 'Yes', otherwise 'No'. Examples: Input : GeeksOutput : YesInput : geeksforgeeksOutput : NoPython regex to find sequences of one upper case letter followed by lower case lettersUsing re.search() To check if the sequence of one upper case
2 min read
Histogram Plotting and stretching in Python (without using inbuilt function)
Prerequisites: OpenCV Python Program to analyze an image using Histogram Histogram of a digital image with intensity levels in the range of 0 to L-1 is a discrete function - h(rk) = nk where rk = kth intensity value and no = number of pixels in the image with rk intensity value. If the image has M rows and N columns, then the total number of pixels
3 min read
Python - Extract Upper Case Characters
Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let’s discuss certain ways in which only uppercase letters can be extracted from a string. Method #1: Using list comprehension + isupper() List comprehension and isupper function
6 min read
Python | Pandas Series.str.lower(), upper() and title()
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Python has some inbuilt methods to convert a string into a lower, upper, or Camel case. But these methods don't work on lists and other multi-st
4 min read
Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase &amp; title)
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find("string", beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by
4 min read
Sum of upper triangle and lower triangle
Given a N x M matrix. The task is to print the sum of upper and lower triangular elements (i.e elements on the diagonal and the upper and lower elements). Examples : Input: {{6, 5, 4}, {1, 2, 5}, {7, 9, 7}}Output:Upper sum is 29Lower sum is 32 Input: {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}Output:Upper sum is 10Lower sum is 14 Recommended Problem Sum of u
6 min read
Arcade inbuilt functions to draw polygon in Python3
The arcade library is a high-tech Python Package with advanced set of tools for making 2D games with gripping graphics and sound. It is Object-oriented and is especially built for Python 3.6 and above versions. Arcade has two inbuilt functions for drawing a polygon: 1. arcade.draw_polygon_outline( ) : This function is used to draw the outline of th
2 min read
Arcade inbuilt functions to draw point(s) in Python3
The Arcade library is a modern Python Module used widely for developing 2D video games with compelling graphics and sound. Arcade is an object-oriented library. It can be installed like any other Python Package. In this article, we will learn what are the arcade inbuilt functions to draw point. Arcade library is a modern framework, which makes draw
3 min read
Pandas - Convert the first and last character of each word to upper case in a series
In python, if we wish to convert only the first character of every word to uppercase, we can use the capitalize() method. Or we can take just the first character of the string and change it to uppercase using the upper() method. So, to convert the first and last character of each word to upper case in a series we will be using a similar approach. F
2 min read
Convert vowels into upper case character in a given string
Given string str, the task is to convert all the vowels present in the given string to uppercase characters in the given string. Examples: Input: str = “GeeksforGeeks”Output: GEEksfOrGEEksExplanation:Vowels present in the given string are: {'e', 'o'}Replace 'e' to 'E' and 'o' to 'O'.Therefore, the required output is GEEksfOrGEEks. Input: str = “eut
7 min read
Article Tags :
Practice Tags :