Given a Line of Text as Input Output The Number of Characters Excluding Spaces Periods Or Commas

In this article you will learn about how to write a Python program to accept a text and output account of number of character excluding space, periods and comma

let's try to solve this using the Python program.

 

Given a Line of Text as Input, Output The Number of Characters Excluding Spaces, Periods, Or Commas


 

Given a Line of Text as Input, Output The Number of Characters Excluding Spaces, Periods, Or Commas

Let's take a variable named as text and ask a user to enter a text using input function call.  then take another variable containing a string that has three characters that is space,  period and comma  as shown below.

text = input("Enter a Text : ")
non_matching_chars = " .,"




Now take another variable named as total to store The Count of number of characters excluding the space, period and comma. Use the for loop to iterate all the latest present in a text, Using if statement check if letter is not present in matching characters then implement the total by one.

total = 0
for letter in text:
   if letter not in non_matching_chars:
       total += 1




Once the for loop is completed,  total will store the account of the number of characters excluding space period are  comma. Use the print()  function call to print the  integer variable total.

print("The Number of Characters Excluding Spaces, Periods, Or Commas is : ", total)





Python Program
======================

text = input("Enter a Text : ")

non_matching_chars = " .,"

total = 0
for letter in text:
   if letter not in non_matching_chars:
       total += 1

print("The Number of Characters Excluding Spaces, Periods, Or Commas is : ", total)






Output
===============
Enter a Text : how are you?
The Number of Characters Excluding Spaces, Periods, Or Commas is :  9




Output
=================
Enter a Text : hello, world.
The Number of Characters Excluding Spaces, Periods, Or Commas is :  10




Output
===============
Enter a Text : I love python.
The Number of Characters Excluding Spaces, Periods, Or Commas is :  11




Conclusion
==================
Execute the above Python program by providing random text as an input to the program and note down the result.

Comment down  to below if you have any Queries related to above my tall program.




Post a Comment

0 Comments