We are given a sentence as input and our task is to count the number of words in a sentence. To solve this in Python, we first take the sentence from the user, then split the sentence into individual words using a suitable method, and finally count and display the total number of words. For example, if the given sentence is “Hello, world! This is Python.” then the program should output 6.
Example input:
"Hello, world! This is Python."
Example output:6
The simplest and most common solution uses Python’s built-in str.split() method to split the sentence on whitespace and then returns the length of the resulting list:
sentence = input("Enter a sentence: ")
words = sentence.split()
print("Number of words:", len(words))
Code language: PHP (php)
Output :
Enter a sentence: Hello world! Welcome to Python.
Number of words: 5
Explaination :
split() without arguments treats any consecutive whitespace (spaces, tabs, newlines) as a single separator and removes leading or trailing spaces. In the code above, the user input is stored in the variable sentence, then sentence.split() converts the sentence into a list of words. Finally, len(words) counts how many items (words) are in that list and prints the result. This ensures accurate word counting even when extra spaces are present.
() works wellsplit() splits on any whitespace sequence (space, tab, newline).If you want to count only alphanumeric word tokens (so "hello," and "hello" count the same), you can use regular expressions to extract word tokens:
import re
def count_words_regex(sentence: str) -> int:
# \w+ matches sequences of word characters (letters, digits, underscore)
# For Unicode-aware word matching you can use the regex module or adjust the pattern
return len(re.findall(r"\w+", sentence))
if __name__ == "__main__":
s = input("Enter a sentence: ")
print("Number of words (regex):", count_words_regex(s))
Code language: PHP (php)
Note (important for accurate word counting):
\w+ treats letters, digits, and underscores as part of a word."abc_123" are counted as a single word.[A-Za-z]+.For learning purposes or special definitions of a word, you can scan the string and count transitions from whitespace to non-whitespace.
Code:
def count_words_manual(sentence: str) -> int:
in_word = False
count = 0
for ch in sentence:
if ch.isspace():
in_word = False
else:
if not in_word:
count += 1
in_word = True
return count
if __name__ == "__main__":
s = input("Enter a sentence: ")
print("Number of words (manual):", count_words_manual(s))
Code language: PHP (php)
Output:
Enter a sentence: Hello world! Welcome to Python.
Number of words: 5
Explaination : This method works similarly to split(), but it gives you complete control over how words are detected. It manually scans each character and counts a word only when a new non-space sequence begins. This approach is useful when you need custom rules for punctuation or special characters. It is mainly used for learning or advanced text-processing scenarios.
"" → 0 words."state-of-the-art") may be counted as one word by split() (no spaces) and as multiple by some regexes depending on pattern. Decide according to your requirement.\w+ treats "abc_123" as one token. Use more specific regex if needed.regex module (which has better Unicode support) or appropriate Unicode-aware patterns.split() creates a list of words (O(k) where k is number of words). The regex method builds a list of matches as well.str.split() for the majority of simple word-count tasks — fast, easy, and matches user expectations in most cases.re.findall(r"\w+", ...) when you want to ignore punctuation.