wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a program to input line(s) of text from the user until enter is pressed. Count the total number of characters in the text (including white spaces), total number of alphabets, total number of digits, total number of special symbols and total number of words in the given text. (Assume that each word is separated by one space).

Open in App
Solution

Program:
userInput = input("Write a sentence: ")
#Count the total number of characters in the text (including white spaces)which is the length of string
totalChar = len(userInput)
print("Total Characters: ",totalChar)
#Count the total number of alphabets,digit and special characters by looping through each character and incrementing the respective variable value
totalAlpha = totalDigit = totalSpecial = 0
for a in userInput:
if a.isalpha():
totalAlpha += 1
elif a.isdigit():
totalDigit += 1
else:
totalSpecial += 1

print("Total Alphabets: ",totalAlpha)
print("Total Digits: ",totalDigit)
print("Total Special Characters: ",totalSpecial)

#Count number of words (Assume that each word is separated by one space)
#Therefore, 1 space:2 words, 2 space:3 words and so on
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1

print("Total Words in the Input :",(totalSpace + 1))


OUTPUT:
Write a sentence: Good Morning!
Total Characters: 13
Total Alphabets: 11
Total Digits: 0
Total Special Characters: 2
Total Words in the Input: 2

flag
Suggest Corrections
thumbs-up
12
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Formula for Sum of N Terms of an AP
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon