Use split() and len():

python

sentence = "Hello world from Python"
word_count = len(sentence.split())
print(word_count)

split() turns the sentence into a list of words, and len() counts them.

If you want to ignore numbers or punctuation, you can filter the split words first:

python

sentence = "I have 3 apples."
word_count = len([w for w in sentence.split() if w.isalpha()])
print(word_count)

That approach is commonly used when you only want alphabetic words.