Exercice 39

Write a Python program that counts the number of words on a string

Solution

def countWord(s):
# convert the string s to a list
L = s.split()
# The number of words in a string s is exactly equal to the len of the list L
return len(L)
# Testing algorithm
s = "Python is the most popular programming language"
print("The number of words in s is :", countWord(s))
# The output is : 'The number of words in s is : 7'
Younes Derfoufi
my-courses.net

Leave a Reply