KnowledgeBoat Logo

Computer Science

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)

Python

Python String Manipulation

16 Likes

Answer

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

Answered By

8 Likes


Related Questions