Friday, 3 January 2025

Day 73: Python Program to Count Number of 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

user_input = input("Enter a string: ")

lowercase_count = count_lowercase_characters(user_input)

print(f"The number of lowercase characters in the string is: {lowercase_count}")

#source code --> clcoding.com

Code Explanation:

def count_lowercase_characters(input_string):
def: This keyword defines a function in Python.
count_lowercase_characters: The name of the function.
input_string: A parameter that represents the string passed to the function when called.

    count = 0
Initializes a variable count to 0. This variable will store the number of lowercase characters as they are counted.

    for char in input_string:
for: A loop that iterates over each character in the input_string.
char: A temporary variable that represents the current character being processed in the string.

        if char.islower():
if: A conditional statement that checks whether the condition is true.
char.islower(): A built-in Python method that returns True if char is a lowercase letter (e.g., 'a', 'z'), and False otherwise.

            count += 1
If the condition char.islower() is True, this line increments the count variable by 1. This tracks the total number of lowercase letters encountered.

    return count
After the loop finishes processing all characters, this line returns the total value of count (the number of lowercase characters) to the caller of the function.

user_input = input("Enter a string: ")
input(): A function that allows the user to type something into the program. It displays the prompt "Enter a string: " and waits for the user to input a value.
user_input: A variable that stores the string entered by the user.

lowercase_count = count_lowercase_characters(user_input)
Calls the function count_lowercase_characters and passes the user's input (user_input) as an argument.
The function processes the input and returns the count of lowercase characters.
The result is stored in the variable lowercase_count.

print(f"The number of lowercase characters in the string is: {lowercase_count}")
print(): Outputs the specified message to the console.
f-string: A formatted string (introduced in Python 3.6) that allows embedding variables directly inside curly braces {}.
Displays the message along with the value of lowercase_count.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (76) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (950) Python Coding Challenge (392) Python Quiz (46) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses