So I've been learning Python for some months now and was wondering how I would go about writing a function that will count the number of times a word occurs in a sentence. I would appreciate if someone could please give me a step-by-step method for doing this.
text=input("Enter your sentence:")
print("'the' appears", text.count("the"),"times")
simplest way to do it
Simplest way:
def count_occurrences(word, sentence):
return sentence.count(word)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With