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

5/5 - (2 votes)
  • 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:

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

Method 1: Using split() and len()

Prerequisites:

  • split() is a built-in method in Python that 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.

Approach: 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 regex

Approach:

  • 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 that represent 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

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Method 3: Using a For Loop

Approach: 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.

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 the string has no characters in it.
    • If the string is not empty, display the result such that the number of word count 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.

Code:

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

Output:

Number of words: 0

Method 4: Using count

Approach: 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 the count() method, 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 cont of all these characters at one go within the count method; hence, you have to compute their occurrences individually 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

Approach: The idea here is similar to the one explained above. The only difference, in this case, is we are using the sum function to compute the resultant 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

TIDBIT:

Summing up numbers is one of those repetitive tasks you need to do over and over again 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. (Official Docs)

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.