Saturday, 4 January 2025

Day 75: Python Program to Count the Number of Digits and Letters in a String

 


def count_digits_and_letters(input_string):

    digit_count = 0

    letter_count = 0

    for char in input_string:

        if char.isdigit():

            digit_count += 1

        elif char.isalpha():

            letter_count += 1

    return digit_count, letter_count

user_input = input("Enter a string: ")

digits, letters = count_digits_and_letters(user_input)

print(f"Number of digits: {digits}")

print(f"Number of letters: {letters}")

#source code --> clcoding.com 

Code Explanation:

def count_digits_and_letters(input_string):
This line defines a function named count_digits_and_letters that takes a single parameter, input_string. The function's purpose is to count the number of digits and letters in the given string.

    digit_count = 0
This line initializes a variable digit_count to 0. It will be used to keep track of the total number of digits in the string.

    letter_count = 0
This line initializes another variable letter_count to 0. It will be used to keep track of the total number of letters in the string.

    for char in input_string:
This line starts a for loop that iterates over each character in the string input_string. The loop processes one character (char) at a time.

        if char.isdigit():
This line checks if the current character (char) is a digit using the isdigit() method. If char is a digit, the condition evaluates to True, and the next line is executed.

            digit_count += 1
If the current character is a digit, this line increments the digit_count variable by 1, adding to the count of digits found so far.

        elif char.isalpha():
If the character is not a digit, this line checks if it is an alphabetic letter using the isalpha() method. If char is a letter, the condition evaluates to True, and the next line is executed.

            letter_count += 1
If the current character is a letter, this line increments the letter_count variable by 1, adding to the count of letters found so far.

    return digit_count, letter_count
After the loop finishes processing all characters in the string, this line returns a tuple containing the final values of digit_count and letter_count.

user_input = input("Enter a string: ")
This line prompts the user to enter a string using the input() function. The user's input is stored in the variable user_input.

digits, letters = count_digits_and_letters(user_input)
This line calls the count_digits_and_letters function with the user-provided string (user_input) as an argument. The function processes the string and returns two values (the count of digits and letters), which are stored in the variables digits and letters.

print(f"Number of digits: {digits}")
This line displays the number of digits found in the string. It uses an f-string to dynamically include the value of the digits variable in the message.

print(f"Number of letters: {letters}")
This line displays the number of letters found in the string. It uses an f-string to dynamically include the value of the letters variable in the message.

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