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)
OutputThe 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)
OutputLower 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)
OutputLower 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)
OutputLower 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)
OutputThe number of lowercase characters is: 10
The number of uppercase characters is: 3
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
Python Program for Convert characters of a string to opposite case Given a string, convert the characters of the string into the opposite case,i.e. if a character is the lower case then convert it into upper case and vice-versa. Examples: Input : geeksForgEeksOutput : GEEKSfORGeEKSInput: hello every oneOutput: HELLO EVERY ONEExample 1: Python Program for Convert ch
6 min read
Python Program to Converts Characters To Uppercase Around Numbers Given a String, the following program converts the alphabetic character around any digit to its uppercase. Input : test_str = 'geeks4geeks is best1 f6or ge8eks' Output : geekS4Geeks is besT1 F6Or gE8Ek Explanation : S and G are uppercased as surrounded by 4.Input : test_str = 'geeks4geeks best1 f6or
8 min read
Python program to capitalize the first and last character of each word in a string In this article, we will explore how to capitalize the first and last character of each word in a string in Python. It involves processing each word to transform its first and last letters to uppercase, while making the other characters lowercase.Using List Comprehensionlist comprehension and string
2 min read
Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is
2 min read
Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read
Find the Length of a String Without Using len Function in Python In this article, we will explore different methods to find the length of a string without using the len() function in Python. We will look at several techniques like loops, recursion, and generator expressions. These approaches help us manually calculate the length of a string.Using sum()sum() with
2 min read