Write a Python Program That Accepts a Sentence And Find The Number of Words Digits Uppercase Letters And Lowercase Letters

Given a sentence as an input to the Python program your task is to write a Python program to find the number of word digits, uppercase letters and lowercase letters present in the given sentence.

Let's use the four counters and keep counting the characters until the end of the sentence.

 

 

Write a Python Program That Accepts a Sentence And Find The Number of Words,Digits,Uppercase Letters And Lowercase Letters



Write a Python Program That Accepts a Sentence And Find The Number of Words, Digits, Uppercase Letters And Lowercase Letters

Let's take a variable named as text to store the given string,  and ask a user to enter a string using the input function call.  Take 4 variables to store The Count of words, digits, uppercase and lowercase count  that is words, digits, upper_count, lower_count. Initialize this for variables with 1, 0, 0, 0.  we are  initializing the word count value as 1  because we are not counting “\n” and that is new line character

text = input("Enter a String : ")
words, digits, upper_count, lower_count = 1, 0, 0, 0





Using the for loop, iterate all the letters present in the string/  sentence.  using the if statement check if a letter is a space “ “  then increment the word count,  using the else if block check if the letter is between “0 and 9”  if so increment the  digit count.  using another  else if block check if the letter is between “A and Z” increment the upper case count.  using another else if log check if letter is between “a and z” then increment the lower case counter.

for letter in text:
   if letter == " ":
       words += 1
   elif "0" <= letter <= "9":
       digits += 1
   elif "A" <= letter <= "Z":
       upper_count += 1
   elif "a" <= letter <= "z":
       lower_count += 1





After the for loop gets complete,  using the four print statements print the number of words digits uppercase letter counter and the lower case letters counter as shown below.

print(f"The Number of Words is : {words}")
print(f"The Number of Digits is : {digits}")
print(f"The Number of UpperCase Letters is : {upper_count}")
print(f"The Number of LowerCase Letters is : {lower_count}")



Code
==============

text = input("Enter a String : ")

words, digits, upper_count, lower_count = 1, 0, 0, 0
for letter in text:
   if letter == " ":
       words += 1
   elif "0" <= letter <= "9":
       digits += 1
   elif "A" <= letter <= "Z":
       upper_count += 1
   elif "a" <= letter <= "z":
       lower_count += 1

print(f"The Number of Words is : {words}")
print(f"The Number of Digits is : {digits}")
print(f"The Number of UpperCase Letters is : {upper_count}")
print(f"The Number of LowerCase Letters is : {lower_count}")





Output
=============
Enter a String : this is a string 1234
The Number of Words is : 5
The Number of Digits is : 4
The Number of UpperCase Letters is : 0
The Number of LowerCase Letters is : 13




Output
============
Enter a String : Write a Python Program That Accepts a Sentence And Find The Number of Words,Digits,Uppercase Letters And Lowercase Letters
The Number of Words is : 18
The Number of Digits is : 0
The Number of UpperCase Letters is : 17
The Number of LowerCase Letters is : 86




Output
===============
Enter a String : Upper Case
The Number of Words is : 2
The Number of Digits is : 0
The Number of UpperCase Letters is : 2
The Number of LowerCase Letters is : 7




Conclusion
==============
Execute the above program by providing the random string as input to the python program. Comment down below if you have any suggestion regarding the above program.




Post a Comment

0 Comments