Sunday, May 1, 2022

Python Program to Count Number of Words in a String

In this post we’ll see how to write a Python program to count number of words in a String. This program can be written in various ways and this post shows some of the ways.

1. If you can’t use any of the methods of the String class then Python program for counting number of words can be written by iterating each character of the string using for loop and check if the character is space (' '), tab('\t') or linefeed ('\n'). If such a character is found that means a new word is starting so the count is incremented by 1.

def number_words(text):
  print('String-', text)
  no_of_words = 1
  for ch in text:
    if (ch == ' ' or ch == '\t' or ch == '\n'):
      no_of_words += 1
  print('Total number of words in String', no_of_words)

number_words('This is a test string')
s = 'This Python program counts\tnumber of words in a String.'
number_words(s)

Output

String- This is a test string
Total number of words in String 5
String- This Python program counts number of words in a String.
Total number of words in String 10

2. Using split() method in Python you can count words in a String. Whitespaces are used as a separator by default in split() method and the method returns a list of the words in the string. By using len() function you can get the length of that list which gives the number of words in a String.

def number_words(text):
  print('Total number of words in String', len(text.split()))
    
number_words('This is a test string')
s = 'This Python program counts\tnumber of words in a String.'
number_words(s)

Output

Total number of words in String 5
Total number of words in String 10

3. You can also write Python program to count number of words in a String using regular expressions in Python. In the program two methods split() and findall() are used.

The split() method splits the string as per the passed regular expression and the split words are returned as a list.

The findall() method returns all occurrences of the matching string as a list.

\s sequence character represents white space, \s+ means 1 or more white spaces.

\W sequence character represents non-alphanumeric, \W+ means 1 or more non-alphanumeric characters.

import re

def number_words(text):
  print('Total number of words in String', len(re.findall(r'\W+', text)))
  print('Total number of words in String', len(re.split(r'\s+', text)))

number_words('This is a Python program')
s = 'This Python program\'s job is to find number of words in a String'
number_words(s)

Output

Total number of words in String 4
Total number of words in String 5
Total number of words in String 13
Total number of words in String 13

That's all for this topic Python Program to Count Number of Words in a String. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Programs Page


Related Topics

  1. Python Program to Count Occurrences of Each Character in a String
  2. Changing String Case in Python
  3. Python String split() Method
  4. Python count() method - Counting Substrings
  5. Python while Loop With Examples

You may also like-

  1. User-defined Exceptions in Python
  2. Namespace And Variable Scope in Python
  3. Multiple Inheritance in Python
  4. ListIterator in Java
  5. TreeMap in Java With Examples
  6. Count Number of Words in a String Java Program
  7. Spring Component Scan Example
  8. Benefits, Disadvantages And Limitations of Autowiring in Spring