Hi, in this article lets work on python function that accepts a string and calculate the number of upper case letters and lower case letters.
You need to pass string as an input argument to function and the function will have two counters to count the number of upper case and lower case letters. The function uses built in function isupper() and islower() to check and increment the counter.
def check_char(data):
upper_count = 0
lower_count = 0
for letter in data:
if letter.isupper():
upper_count += 1
if letter.islower():
lower_count += 1
print("Upper Count is : ", upper_count)
print("Lower Count is : ", lower_count)
Now once we have he function lets ask user to enter a string and what ever user enters the text just pass it on to the function as input argument.
text = input("Enter a String : ")
check_char(text)
Complete Program :
def check_char(data):
upper_count = 0
lower_count = 0
for letter in data:
if letter.isupper():
upper_count += 1
if letter.islower():
lower_count += 1
print("Upper Count is : ", upper_count)
print("Lower Count is : ", lower_count)
text = input("Enter a String : ")
check_char(text)
Output 1 :
# python main.py
Enter a String : How Are You ?
Upper Count is : 3
Lower Count is : 6
Lower Count is : 6
Output 2 :
# python main.py
Enter a String : Earth is Flat
Upper Count is : 2
Lower Count is : 9
Lower Count is : 9
Conclusion : Try running the program by yourself, let me know if any issues in the comment section.
Keywords : Write a Python Function That Accepts a String And Calculate The Number of Upper Case Letters And Lower Case Letters
0 Comments