You can use Python's built-in split()
string method to accomplish this.
It works like this:
>>> string = "hello world how are you"
>>> string.split()
['hello', 'world', 'how', 'are', 'you']
The length of the returned list is the word count. So we end up with:
def count_words(string):
return len(string.split())
And testing it:
>>> count_words(string)
5