HomePythonPython Program to Count Number of Lowercase Characters in a String

Python Program to Count Number of Lowercase Characters in a String

This Python program counts the number of lowercase characters in a string.

Problem Statement

You are tasked with writing a Python program that counts the number of lowercase characters in a given string.

Python Program to Count Number of Lowercase Characters in a String

# Function to count lowercase characters in a string
def count_lowercase_characters(input_string):
    count = 0
    for char in input_string:
        if char.islower():
            count += 1
    return count

# Input string
input_string = input("Enter a string: ")

# Call the function to count lowercase characters
lowercase_count = count_lowercase_characters(input_string)

# Display the result
print("Number of lowercase characters:", lowercase_count)

How it works

Certainly! Here’s how the Python program to count the number of lowercase characters in a string works:

  1. User Input:
    • The program starts by prompting the user to enter a string. This input is obtained using the input() function, which waits for the user to type in a string and press Enter.
  2. Function Definition:
    • The program defines a function called count_lowercase_characters(input_string). This function takes the user’s input string as its argument.
  3. Counting Lowercase Characters:
    • Inside the function, there is a variable called count initialized to 0. This variable will be used to keep track of the count of lowercase characters.
    • The program then enters a loop that iterates through each character in the input string one by one. This is done using a for loop.
    • For each character, it checks whether it is a lowercase character using the islower() method. If the character is lowercase, it increments the count variable by 1.
  4. Return Count:
    • After looping through all the characters in the input string, the function returns the value of count, which represents the count of lowercase characters in the input string.
  5. Main Execution:
    • Back in the main part of the program, the user’s input string is obtained.
    • The count_lowercase_characters() function is called with the input string as its argument, and the result is stored in a variable called lowercase_count.
  6. Display Result:
    • Finally, the program displays the result by printing the count of lowercase characters along with an informative message.

Here’s a step-by-step breakdown of the program’s execution:

  • User enters a string, e.g., “Hello World.”
  • The program calls count_lowercase_characters("Hello World").
  • Inside the function, it counts the lowercase characters (‘e’, ‘l’, ‘l’, ‘o’, ‘o’, ‘r’, ‘l’, ‘d’) and returns 6.
  • The program prints “Number of lowercase characters: 6” as the output.

This program effectively counts the lowercase characters in a given string and provides the count as the output.

Input/Output

Python Program to Count Number of Lowercase Characters in a String

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...