Count words and spaces with a multiple line text


Keywords:python 


Question: 

I need to count the number of words and the number of spaces of the text indicated below through defining a function in Python. I tried with the split(" ") command but due to the text are in two different lines I don't know how to do it. Can somebody help me?

text1 = "A sentence is a group of words that makes complete sense, \n contains a main verb, and begins with a capital letter."


2 Answers: 

You can try the following

words

print len(text1.replace('\\', '').split())

spaces

print text1.count(' ')


Another way to count the number of words it's the following:

print len(text1.split())