5.1k views
0 votes
Write a function named word_count that accepts a string as its parameter and returns the number of words in the string. A word is a sequence of one or more non-space characters (any character other than ' ').

1 Answer

4 votes

Answer:

def word_count(words):

return len(words.split(" "))

print(word_count("This is Python."))

Step-by-step explanation:

*The code is in Python.

Create a function called word_count that takes one parameter, words

Split the words using split function, use " " for the delimiter, and return the length of this using len function.

Note that split function will give you a list of strings that are written with a space in the words. The len function will just give you the length of this list.

Call the word_count function with a string parameter and print the result.

User Matthew Benedict
by
6.4k points