CBSE Class 11 Computer Science Question 87 of 91

String Manipulation — Question 11

Back to all questions
11
Question

Question 11

Write a program that asks the user for a string (only single space between words) and returns an estimate of how many words are in the string. (Hint. Count number of spaces)

Solution
str = input("Enter a string: ")
count = 0
for ch in str :
    if ch.isspace() :
        count += 1
print("No of words =", (count + 1))
Output
Enter a string: Python was conceived in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands.
No of words = 20
Answer