How to Count Upper and Lower Case Characters in Python

In this tutorial, we will program "How to Count Upper and Lower Case Characters in Python." We will learn how to properly count the lowercase and uppercase characters present in a string. The objective is to correctly determine the number of uppercase and lowercase characters in the string. I will provide a sample program to demonstrate the coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you'll be able to do it yourself with ease. The program I’ll show you demonstrates the proper way to count the uppercase and lowercase characters present in a string. I'll do my best to provide a simple method for calculating this. So, let's start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. ret = False
  2.  
  3. while True:
  4.         print("\n================= Count Upper and Lower Case Characters =================\n\n")
  5.  
  6.         string = input("Enter a string: ")
  7.         upper = 0
  8.         lower = 0
  9.         up="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  10.         lo="abcdefghijklmnopqrstuvwxyz"
  11.         for i in string:
  12.                 if i in up:
  13.                         upper+=1
  14.                 elif i in lo:
  15.                         lower+=1
  16.         print('Lower case characters = %s' %lower)
  17.         print('Upper case characters = %s' %upper)
  18.  
  19.         opt = input("\nDo you want to try again?(yes/no): ")
  20.  
  21.         if opt.lower() == 'yes':
  22.                 ret=False
  23.         elif opt.lower() == 'no':
  24.                 ret=True
  25.                 print("Exiting program....")
  26.  
  27.         else:
  28.                 print("Please enter yes/no:")
  29.                 break
  30.  
  31.         if ret == False:
  32.                 continue

This Python script counts the number of uppercase and lowercase characters in a user-provided string. It uses two loops to compare each character in the string against predefined uppercase and lowercase alphabets, incrementing counters accordingly. After displaying the counts, the program asks if the user wants to try again, and continues in a loop until the user chooses to exit.

Output:

There you have it we successfully created How to Count Upper and Lower Case Characters in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment