हिंदी

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), the total number of alphabets

Advertisements
Advertisements

प्रश्न

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), the total number of alphabets, the total number of digits, the total number of special symbols, and the total number of words in the given text. (Assume that each word is separated by one space).

संक्षेप में उत्तर
Advertisements

उत्तर

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 
shaalaa.com
String Handling in Python
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 8: Strings - Exercise [पृष्ठ १८८]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 11
अध्याय 8 Strings
Exercise | Q 1. | पृष्ठ १८८
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×