Write a Python Program That Takes a Sentence as Input And Displays The Number of Words Number of Capital Letter Number of Small Letter And Number of Special Symbols in The Inputted Sentence

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program That Takes A Sentence As Input And Displays The Number Of Words Number Of Capital Letter Number Of Small Letter And Number Of Special Symbols In The Inputted Sentence

 

Title : 

Write A Python Program That Takes A Sentence As Input And Displays The Number Of Words Number Of Capital Letter Number Of Small Letter And Number Of Special Symbols In The Inputted Sentence 

 

 

Write a Python Program That Takes a Sentence as Input And Displays The Number of Words Number of Capital Letter Number of Small Letter And Number of Special Symbols in The Inputted Sentence

 

Description : 

Hi guys, in this article you will learn The Python program that accepts a string from the user and then try to count the number of words, capital letters, small letters and special characters present in that particular sentence. Let's get to program, take a variable named as sentence and use input function call to accept a sentence. Now use the four variables named as words, caps, small_letters, special_chars and initialize them to 1, 0, 0, 0 Respectively. the reason we are initializing word to one if we are assuming the string contains at least one word


    

sentence = input("Enter Sentence : ")

words, caps, small_letters, special_chars = 1, 0, 0, 0


    


Use the for loop to iterate all the letters present in the sentence. inside the for loop use the if statement to check if letter is a space, then increment the word Counter by 1. Now use another else if statement to check if the letter is between A to Z, increment the caps by 1. Now use another Elsa statement to check if the letter is between a to z, Then increment the small letter counter by 1. Use the else Block to increment the special character by 1.


    

for letter in sentence:
   if letter == " ":
       words += 1
   elif "A" <= letter <= "Z":
       caps += 1
   elif "a" <= letter <= "z":
       small_letters += 1
   else:
       special_chars += 1

print(f"The Number of Words is : {words}")
print(f"The Number of Capital Letters is : {caps}")
print(f"The Number of Small Letters is : {small_letters}")
print(f"The Number of Special Character is : {special_chars}")


    




Complete Python Code : 


    

sentence = input("Enter Sentence : ")

words, caps, small_letters, special_chars = 1, 0, 0, 0

for letter in sentence:
   if letter == " ":
       words += 1
   elif "A" <= letter <= "Z":
       caps += 1
   elif "a" <= letter <= "z":
       small_letters += 1
   else:
       special_chars += 1

print(f"The Number of Words is : {words}")
print(f"The Number of Capital Letters is : {caps}")
print(f"The Number of Small Letters is : {small_letters}")
print(f"The Number of Special Character is : {special_chars}")


    

 

 

Output : 


    

Enter Sentence : print(f"The Number of Special Character is : {special_chars}")
The Number of Words is : 8
The Number of Capital Letters is : 4
The Number of Small Letters is : 43
The Number of Special Character is : 8


    

 

Output : 


    

Enter Sentence : code with tj
The Number of Words is : 3
The Number of Capital Letters is : 0
The Number of Small Letters is : 10
The Number of Special Character is : 0


    



Conclusion :

The above Python program accepts string from the user and iterates all the letters present in that particular string While it iterates all the letters present in the string it checks for the spaces then increments the word counter, It also checks for the caps letters and small letters then implements their respective counter Appropriately. Then if it doesn't find any of the spaces or English alphabets it increments the special character counter Comment it down below if you have any query is regarding the above Python program To support me please subscribe to my YouTube channel, so you don't miss out new content https://www.youtube.com/channel/UCUooZAqgfE4ngJ0oXsj3-MA

 

Post a Comment

0 Comments