How to Count the Number of Words in a String in Python

  • You can count the number of words in string using one of the following options:
    • Method 1: Using split() and len()
    • Method 2: Using regex
    • Method 3: Using a For Loop
    • Method 4: Using count
    • Method 5: Using sum

Problem Formulation

Given a string – sentence.

How many words does the string sentence have within it?

Examples:

INPUT
sentence = "Finxter helps you to master Python."
OUTPUT
Number of words: 6

INPUT
sentence = """Lorem ipsum dolor sit amet.
Consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
OUTPUT
Number of words: 19

INPUT
sentence=""
OUTPUT
Number of words: 0

Video Explanation:

Method 1: Using split() and len()

Prerequisites: You can use the split() and len() function to accomplish this easily:

  • The Python string method split() splits the string at a given separator and returns a split list of substrings. Read more here.
  • Python’s built-in function len() returns the length of the given string, array, list, tuple, dictionary, or any other iterable. The type of the return value is an integer that represents the number of elements in this iterable. Read more here.

To separate out each word in the given string, use the split() method. This returns a list containing all the words of the given string. Then find the length of this list with the help of the len() method to return the number of words in the given string.

sentence = "Finxter helps you to master Python"
words = len(sentence.split())
print("Number of words:", words)

Output:

Number of words: 6

Method 2: Using Regular Expressions

You can find the number of words in a sentence by using the expression len(re.findall(r'\w+', sentence)) combining the regex.findall() function with the len() function. The pattern r'\w+' matches the words in the sentence.

💡 Info: In Python regex, the metacharacter \w matches any alphanumeric character (letters and digits) and the underscore _. It is equivalent to the character set [a-zA-Z0-9_]. The \w metacharacter is often used to match variable names, identifiers, and other types of alphanumeric patterns.

Python’s re.findall(pattern, string) determines the number of matches of a given pattern in a string. Use a simple word character pattern match (“\w“) to find all the strings representing a word in the given string.

The result is a list of matches containing the words present in the given string—the length of the list is the number of occurrences of the words in the given string leading to the solution.

Syntax: len(re.findall(r'\w+', sentence))

Code:

import re
sentence = "Finxter helps you to master Python"
words = len(re.findall(r'\w+', sentence))
print("Number of words:", words)

Output:

Number of words: 6

👉 Recommended: Python Regex Superpower [Full Tutorial]

Method 3: Using a For Loop

The idea here is to find the number of occurrences of the space character, the tab character, and the newline character and then find their sum to deduce the output. This is based on the fact that counting the total number of words in a given string will return the number of space/tab/newline-separated substrings.

sentence = ""
words = 0
for i in sentence:
    if i == " " or i == "\t" or i == "\n":
        words += 1
if len(sentence) > 0:
    print("Number of words:", words + 1)
else:
    print("Number of words: 0")

Explanation:

  • Initialize a counter that will trace the number of words in the given string.
  • Use a for loop to iterate across all the characters of the given string.
  • As soon as a space or a tab or a new line character is found, increment the counter variable.
  • Finally, check if the given string is empty or not:
    • If the given string is empty, display 0 as the output since it has no characters.
    • If the string is not empty, display the result such that the number of word counts will be given by incrementing the calculated value of the counter by 1. This is because if a certain string has three words, then it will be separated by two spaces. Hence, the number of words is always one more than the number of spaces in the given sentence.
      • Example: Given a string: "One Two Three." Here the number of spaces is two, whereas the number of words is three.

Output:

Number of words: 0

Method 4: Using count

Python’s str.count(sub) method counts the number of non-overlapping occurrences of a substring. Thus, if you count the number of occurrences of newline characters, spaces, and tabs present in the given string with the help of count(), it will return the total number of words present in the given string. Note that the value returned by the count variable will be one less than the total number of words in the given string. So, you must add one to the computed value before returning the output.

Code:

sentence = """Lorem ipsum dolor sit amet.
Consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
spaces = sentence.count(' ')
tabs = sentence.count('\t')
newlines = sentence.count('\n')
words = spaces+tabs+newlines
if sentence:
    print("Number of words: ", words+1)
else:
    print("Number of words: 0")

Output:

Number of words: 19

Explanation:

  • Use the count() method to find the number of occurrences of space, tab, and newline characters in the string. Since you cannot compute the count of all these characters at one go within the count method; you compute their occurrences one by one. You can then simply add each occurrence to find the net value of their occurrence together in the given string.
  • Finally, check if the given string is empty or not:
    • If the given string is empty, display 0 as the output since the string has no characters in it.
    • If the given string is not empty – return the output by adding one to the sum of values computed by the count methods.

Method 5: Using sum()

The idea here is similar to the previous Method 4. The only difference, in this case, is we are using the sum() function to compute the resulting count of (space characters+tab characters+newline characters) in a single line of code.

Code:

sentence = """Lorem ipsum dolor sit amet.
Consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
if sentence:
    x = sum(1 for c in sentence if c in ' \t\n')
    print("Number of words: ", x+1)
else:
    print("Number of words: 0")

Output:

Number of words: 19

Summing up numbers is one of those repetitive tasks you need to do repeatedly in your practical code projects.

To help you accomplish this task in a concise, readable, and efficient way, Python’s creators have added the built-in sum() function. It sums over all elements in a Python list—or any other iterable for that matter.

👉 Read Here: Python sum() – A Simple Illustrated Guide


Recommended Reads:

💎Web Scraping with BeautifulSoup

One of the most sought-after skills on Fiverr and Upwork is web scraping . Make no mistake: extracting data programmatically from websites is a critical life-skill in today’s world that’s shaped by the web and remote work. This course teaches you the ins and outs of Python’s BeautifulSoup library for web scraping.