Alphabet

Python Program for count upper or lowercase letter of the given string

Python Program for count upper or lowercase letter of the given string

In this article, we will discuss the concept of the Python Program for count upper case or lowercase letter of the given string in Python language

In this post, we are going to learn how to count upper and lower case letters in a given string in Python programming language

Count upper case lower case

Program for count upper or lowercase letter using for loop

The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using for loop in Python language

Program 1

#Python program to count upper case or lower case letter in given string
str=input("Enter a string: ")
upper=0
lower=0

for i in range(len(str)):

      #to lower case letter
      if(str[i]>='a' and str[i]<='z'):
          lower+=1

      #to upper case letter
      elif(str[i]>='A' and str[i]<='Z'):
          upper+=1
print('Lower case letters= ',lower)
print('Upper case letters=' ,upper)

When the above code is executed, it produces the following result

Enter a string: CodeForCoding
Lower case letters= 10
Upper case letters= 3

 

Approach

  1. Declare a String variable as str;
  2. Declare and initialize two integer counter-variable as int upper=0 and lower=0;
  3. The user asked to enter a string to count upper case and lower case letters
  4. A for-loop is used to count total upper case and lower case of the given string
  5. Use an if statement to test lower case, if the test expression is true,  lower becomes lower + 1    (lower=lower+1)
  6. When the if-statements is false, The control moves to elif and checks the test expression of elif
  7. If the test expression of else-if is true,  upper becomes upper + 1(upper =upper +1)
  8. If the test expression of else if is false, control terminates from the loop
  9. Finally, the program displays the number of upper case and lowercase letters of the given string.

 

Python Program for count upper or lowercase letter using ASCII value

The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using ASCII value in Python language

Program 2

str=input("Enter a string: ")
upper=0
lower=0

for i in range(len(str)):

      #to lower case letter
      if(ord(str[i])>=97 and ord(str[i])<=122):
          lower+=1

      #to upper case letter
      elif(ord(str[i])>=65 and ord(str[i])<=90):
          upper+=1
print('Lower case letters= ',lower)
print('Upper case letters=' ,upper)

When the above code is executed, it produces the following result

Enter a string: Python Language
Lower case letters= 12
Upper case letters= 2

 

Approach

  1. Declare a String variable as str;
  2. Declare and initialize two integer counter-variable as int upper=0 and lower=0;
  3. The user asked to enter a string to count upper case and lower case letters
  4. A for-loop is used to count total upper case and lower case of the given string
  5. Use an if statement to test lower case using ASCII value, if the test expression is true,  lower becomes lower + 1    (lower=lower+1)
  6. When the if-statements is false, The control moves to elif and checks the test expression of elif
  7. If the test expression of else-if is true, the upper becomes upper + 1(upper =upper +1)
  8. If the test expression of else if is false, control terminates from the loop
  9. Finally, the program displays the number of upper case and lowercase letters of the given string.

 

Program for count upper or lowercase letter using the build-in function

The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using the predefined function in Python language

Program 3

str =input("Enter a string: ")
upper=0
lower=0
for i in str:
    if (i.islower()):
        lower=lower+1
    elif (i.isupper()):
        upper=upper+1

print("The lower case letters:",lower)
print("The upper case letters:",upper)

When the above code is executed, it produces the following result

Enter a string: python
The lower case letters: 5
The upper case letters: 1

 

Suggested for you

For loop in Python

If statements in Python

operator in Python

Data type in Python

 

Similar post

Java Program for count upper or lowercase letter of the given string

C Program for count upper or lowercase letter of the given string

Program for count upper or lowercase letter of the given string

Python Program for count upper or lowercase letter of the given string

Program to count uppercase and lowercase letter of given string in Java
Python program: Reverse a given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago