#Recall in Unit 3 you wrote a function that would count the #number of words in a string using loops. Now that you know #something about string methods, though, let's do that again #using a different approach. # #Write a function called "num_words" that accepts a string #as an argument and returns the number of words in the #string. You can assume all words are separated by a space, #and that the string has at least one word. You do not need #to worry about punctuation.

Respuesta :

Answer:

#function to count number of words in a string

def num_words(str1):

# using split() to count words in string

res = len(str1.split())

return res

test_string = input("Enter string:")

# printing original string

print ("The original string is : " + test_string)

# printing result

print("Number of word in entered string:")

print (num_words(test_string))

Explanation: