
Exercise for reference:
Create a function that takes any string as input and returns the number of words for that string.
Answer:
def count_words(strng):
strng_list = strng.split()
return len(strng_list)
print(count_words("Hey there it's me!"))
Explanation:
We're using split here which is a string method that splits a string into several strings given a separator passed inside the brackets. When you don't pass a separator, split will split a string at white spaces. This will output a list of strings..Applying len to that list returns the number of listitems, so the number of words.