Friday, 3 January 2025

Day 72: Python Program to Count the Number of Words and Characters in a String



def count_words_and_characters(input_string):

    num_characters = len(input_string)

    words = input_string.split()

    num_words = len(words)

     return num_words, num_characters

input_string = input("Enter a string: ")

num_words, num_characters = count_words_and_characters(input_string)

print(f"Number of words: {num_words}")

print(f"Number of characters (including spaces): {num_characters}")

#source code --> clcoding.com 

Code Explanation:

Function Definition
def count_words_and_characters(input_string):
Purpose: Defines a function named count_words_and_characters to calculate the number of words and characters in a given string.
Parameter:
input_string: The string whose words and characters are to be counted.

Character Count
    num_characters = len(input_string)
Logic: Uses Python’s built-in len() function to determine the total number of characters in the string, including spaces and punctuation.
Result: The length of input_string is stored in the variable num_characters.

Word Count
    words = input_string.split()
    num_words = len(words)

Splitting the String:
The split() method splits the string into a list of words, separating at whitespace (spaces, tabs, or newlines).
For example, "Hello World!" becomes ['Hello', 'World!'].
The resulting list is stored in the variable words.

Counting Words:
The len() function is used again, this time to count the number of elements in the words list.
The result (number of words) is stored in the variable num_words.

Return Statement
    return num_words, num_characters
Logic: Returns two values:
num_words: The total number of words in the input string.
num_characters: The total number of characters in the input string.

Input Handling
input_string = input("Enter a string: ")
Purpose: Prompts the user to input a string and stores the input in the variable input_string.

Function Call
num_words, num_characters = count_words_and_characters(input_string)
Logic: Calls the count_words_and_characters function, passing the input_string as an argument.
Result: The returned values (number of words and characters) are unpacked into num_words and num_characters.

Output
print(f"Number of words: {num_words}")
print(f"Number of characters (including spaces): {num_characters}")
Purpose: Prints the results in a user-friendly format, showing the number of words and the number of characters (including spaces).

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