Python program to count upper and lower case characters without using inbuilt functions
Last Updated :
28 Jan, 2023
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
def upperlower(string):
upper = 0
lower = 0
for i in range ( len (string)):
if ( ord (string[i]) > = 97 and
ord (string[i]) < = 122 ):
lower + = 1
elif ( ord (string[i]) > = 65 and
ord (string[i]) < = 90 ):
upper + = 1
print ( 'Lower case characters = %s' % lower,
'Upper case characters = %s' % upper)
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' ):
l = l + 1
if (i> = 'A' and i< = 'Z' ):
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
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)
Please Login to comment...