Python program to count the number of spaces in string

In this article, we will learn how to count the number of spaces in a string using various Python methods. Counting spaces is useful for text analysis, validation, and formatting tasks.

Methods Used

The following are the various methods to accomplish this task ?

  • Using for loop with indexing

  • Using count() function

  • Using isspace() function

  • Using Counter() function

  • Using countOf() function from operator module

Method 1: Using For Loop with Indexing

This method iterates through each character using index positions and counts spaces manually ?

def countSpaces(inputString):
    spaces_count = 0
    
    for index in range(len(inputString)):
        if inputString[index] == " ":
            spaces_count += 1
    
    return spaces_count

inputString = "tutorialspoint is a best learning platform"
result = countSpaces(inputString)
print("Count of spaces:", result)
Count of spaces: 6

Method 2: Using count() Function

The count() method returns the number of occurrences of a substring in the string. This is the most straightforward approach ?

Syntax

string.count(value, start, end)

Example

def countSpaces(inputString):
    return inputString.count(" ")

inputString = "hello tutorialspoint python"
result = countSpaces(inputString)
print("Count of spaces:", result)
Count of spaces: 2

Method 3: Using isspace() Function

The isspace() method returns True if a character is a whitespace character. This method handles all types of whitespace ?

Syntax

string.isspace()

Example

inputString = "hello tutorialspoint python codes"
spaces_count = 0

for char in inputString:
    if char.isspace():
        spaces_count += 1

print("Count of spaces:", spaces_count)
Count of spaces: 3

Method 4: Using Counter() Function

The Counter() function from the collections module counts all characters in a string and returns their frequencies as a dictionary ?

from collections import Counter

inputString = "hello tutorialspoint python codes"
frequency = Counter(inputString)
spaces_count = frequency[' ']

print("Count of spaces:", spaces_count)
Count of spaces: 3

Method 5: Using countOf() Function

The countOf() function from the operator module counts occurrences of a value in any iterable ?

import operator as op

def countSpaces(inputString):
    return op.countOf(inputString, " ")

inputString = "hello tutorialspoint python"
result = countSpaces(inputString)
print("Count of spaces:", result)
Count of spaces: 2

Comparison

Method Performance Best For
count() Fast Simple space counting
isspace() Moderate All whitespace types
Counter() Moderate Multiple character frequencies
For loop Slow Learning purposes
countOf() Fast General purpose counting

Conclusion

The count() method is the most efficient for counting spaces, while isspace() handles all whitespace types. Choose Counter() when you need frequencies of multiple characters in the same operation.

Updated on: 2026-03-26T23:58:48+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements