Scrollable Nav Bar

Python program to count the number of words in a sentence

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

Python program using split() method

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.

Why split() works well

  • Default split() splits on any whitespace sequence (space, tab, newline).
  • It ignores extra spaces between words and leading/trailing whitespace.
  • Simple and fast (O(n) time where n is the length of the string).

Counting words while ignoring punctuation

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):

  • The regex pattern \w+ treats letters, digits, and underscores as part of a word.
  • Because of this, strings like "abc_123" are counted as a single word.
  • If your requirement is to count only alphabetic words, use a stricter pattern such as [A-Za-z]+.
  • For multilingual or Unicode text, consider using a Unicode-aware regex pattern for better SEO-friendly text processing results.

Manual approach

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.


Edge cases and notes

  • Empty string "" → 0 words.
  • Strings with only spaces or newlines → 0 words.
  • Hyphenated 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.
  • Numbers and underscores: \w+ treats "abc_123" as one token. Use more specific regex if needed.
  • Unicode: For languages beyond ASCII, consider using the third-party regex module (which has better Unicode support) or appropriate Unicode-aware patterns.

Time and space complexity

  • All presented solutions run in O(n) time where n is the length of the input string (they scan the input once).
  • Space: split() creates a list of words (O(k) where k is number of words). The regex method builds a list of matches as well.

When to use which method

  • Use str.split() for the majority of simple word-count tasks — fast, easy, and matches user expectations in most cases.
  • Use re.findall(r"\w+", ...) when you want to ignore punctuation.
  • Use manual scanning when you need custom word boundaries or want to minimize allocations.