How would I go about counting the words in a sentence? I\'m using Python.
For example, I might have the string:
string = \"I am having a very
This is a simple word counter using regex. The script includes a loop which you can terminate it when you're done.
#word counter using regex
import re
while True:
string =raw_input("Enter the string: ")
count = len(re.findall("[a-zA-Z_]+", string))
if line == "Done": #command to terminate the loop
break
print (count)
print ("Terminated")
You can use regex.findall():
import re
line = " I am having a very nice day."
count = len(re.findall(r'\w+', line))
print (count)