3 ways in Python to count the words and characters of a string

Introduction:

In this Python programming tutorial, we will learn how to count the total number of words and characters in a given string. The program will take the string as input from the user, count the words and characters, and then it will print out the result.

Method 1: By splitting the string:

With this approach, we will split the string into words to count the total number of words. The following algorithm will use in this program:

  1. Ask the user to enter a string.
  2. Split the string into words and save it in a variable.
  3. Calculate the total number of words, i.e. the total count of words in the string.
  4. Iterate over the words of the string one by one. For each word, count the number of characters and add that value to a final word counter variable. We need to initialize this variable as 0 at the start of the program.
  5. Print out the calculated total count of words and characters to the user.

Python program to count the words and characters in a string by splitting the string:

# 1
word_count = 0
char_count = 0

# 2
usr_input = input("Enter a string : ")

# 3
split_string = usr_input.split()

# 4
word_count = len(split_string)

# 5
for word in split_string:
    # 6
    char_count += len(word)

# 7
print("Total words : {}".format(word_count))
print("Total characters : {}".format(char_count))

Download this program on Github

python count words and characters in string

Explanation :

The commented numbers in the above program denote the step numbers below:

  1. Create two variables to store the final word count and the character count. The variables are word_count and char_count. Initialize these variables as 0.
  2. Ask the user to enter a string. Read and assign the string to the usr_input variable.
  3. Split the user-provided string with the split() method. It will split the string into words and return one list with these words.
  4. Count the total number of words in the list. Assign this to the variable word_count.
  5. By using a for loop, we are iterating over the words of the split_string.
  6. While iterating over the list, add the length of each word to the variable char_count. We are using the len() method to find the length of each word.
  7. The word count is assigned to the word_count variable, and the character count is assigned to the char_count variable. Print out these values to the user.

Sample Output :

Enter a string : Hello world
Total words : 2
Total characters : 10

Enter a string : The quick brown fox jumps over the lazy dog
Total words : 9
Total characters : 35

Enter a string : abc def gh ij k l mno pqr stu vwx yz
Total words : 11
Total characters : 26

Enter a string : a b c d e f g h i j k l m n o p q r s t u v w x y z
Total words : 26
Total characters : 26

python count words and characters in string

Method 2: With list comprehension:

The above example is using a for loop to calculate the length of the words. With list comprehension, we can calculate the character count of each word in a simpler and more concise way. Let’s write the program:

word_count = 0
char_count = 0
last_word_space = False

usr_input = input("Enter a string : ")
word_list = usr_input.split()

word_count = len(word_list)
char_count = sum([len(c) for c in word_list])

print("Total words : {}".format(word_count))
print("Total characters : {}".format(char_count))

Download this program on Github

Here, we are calculating the word count by using the split() and the len() methods. The character count is calculated by adding the length of each word in the list. The word count and character count values are assigned to the word_count and char_count variables respectively. If you run this program, it will print similar output.

Enter a string : a bc de
Total words : 3
Total characters : 5

Method 3: By using regular expression:

With a regular expression, we can filter out the words of a string and get one list of all the words. The total number of words and the total number of characters can be calculated easily if we filter out the words. For example:

import re

word_count = 0
char_count = 0
last_word_space = False

usr_input = input("Enter a string : ")
word_list = re.findall(r"\w+", usr_input)

word_count = len(word_list)
char_count = sum([len(c) for c in word_list])

print("Total words : {}".format(word_count))
print("Total characters : {}".format(char_count))

Download this program on Github

  • The program is using the re module to use the regular expression.
  • The findall method is finding all the words of the string with the regular expression. It returns one list of words and it is assigned to the word_list variable.
  • The total word count is equal to the length of the word_list. The len() method is used to find the length of the word list.
  • The character count is calculated by finding the sum of the lengths of the words in the list. It is assigned to the char_count variable.

If you run this program, it will print similar output.

Enter a string : a bc   fl    lk
Total words : 4
Total characters : 7

Conclusion :

In this tutorial, we have learned how to find the count of words and characters in a string in Python. It is one of the most commonly used programs in Python or any other programming language. You can use a similar approach to count the total number of words and characters in a file.

Similar tutorials :