Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to count the number of spaces in string
In this article, we will a python program to count the number of spaces in a string.
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 of the operator module
Assume we have taken an input string. we will now count the number of spaces in an input string above methods.
Method 1: Using for loop (with indexing)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a function countSpaces() to return the count of the number of spaces in a string by accepting the input string as an argument.
Initialize a variable with 0 to store the total count of the number of spaces.
Use the for loop, to traverse till the length of the string using len() function(returns the number of items in an object).
Use the if conditional statement to check whether each character of a string is blank/space or not.
Incrementing the count value by 1 if the above condition is true
Return the count of spaces in an input string.
Create a variable to store the input string.
Call the above-defined countSpaces() function by passing the input string as an argument.
Example
The following program returns the number of spaces in an input string using for loop (with indexing) -
# function to return the count of no of spaces in a string
# by accepting the input string as an argument
def countSpaces(inputString):
# storing the count of the number of spaces in a given string
spaces_count = 0
# Traversing till the length of the string
for index in range(0, len(inputString)):
# checking whether each character of a string is blank/space or not
if inputString[index] == " ":
# incrementing the space value count by 1
spaces_count += 1
# returning the count of the number of spaces in an input string
return spaces_count
# input string
inputString = "tutorialspoint is a best learning platform for coding"
# calling the above defined countSpaces() function by
# passing input string as an argument
print("Count of no of spaces in an input string:", countSpaces(inputString))
Output
On executing, the above program will generate the following output -
Count of no of spaces in an input string: 7
Method 2: Using count() function
count() function ? Returns the no. of times the given value appears in a string.
Syntax
string.count(value, start, end)
Example
The following program returns the number of spaces in an input string using the count() function -
# creating a function to return the count of no of spaces in a string
# by accepting the input string as an argument
def countSpaces(inputString):
# returing the spaces count in a string using the count() function
return inputString.count(" ")
# input string
inputString = "hello tutorialspoint python"
# calling the above defined countSpaces() function by
# passing input string as an argument
print("Count of no of spaces in an input string:",countSpaces(inputString))
Output
On executing, the above program will generate the following output -
Count of no of spaces in an input string: 2
Method 3: Using isspace() function
isspace() function ? returns True if all the characters present in a string are whitespaces, else False.
Syntax
string.isspace()
Example
The following program returns the number of spaces in an input string using isspace() function -
# input string
inputString = "hello tutorialspoint python codes"
# storing the count of spaces
spaces_count = 0
# traversing through each character of the input string
for c in inputString:
# checking whether the current character is space or not using isspace() function
if(c.isspace()):
# incrementing the spaces_count value by 1 if the condition is true
spaces_count += 1
# printing the count of no of spaces in an input string
print("Count of no of spaces in an input string:", spaces_count)
Output
On executing, the above program will generate the following output -
Count of no of spaces in an input string: 3
Method 4: Using Counter() function
Counter() function ? a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.
Here the Counter() function returns the frequency of each character of the input string as a key-value pair.
Example
The following program returns the number of spaces in an input string using the Counter() function -
# importing a Counter function from the collections module
from collections import Counter
# input string
inputString = "hello tutorialspoint python codes"
# getting the frequency of each character of the string as a
# key-value pair using Counter() function
frequency = Counter(inputString)
# getting the frequency/count of spaces
spaces_count = frequency[' ']
# printing the count of no of spaces in an input string
print("Count of no of spaces in an input string:", spaces_count)
Output
On executing, the above program will generate the following output -
Count of no of spaces in an input string: 3
Method 5: Using countOf() function of the operator module
Example
The following program returns the number of spaces in an input string using countOf() function of the operator module -
# importing operator module with alias name op
import operator as op
# creating a function to return the count of no of spaces in a string
# by accepting the input string as an argument
def countSpaces(inputString):
# returing the spaces count in a string using the countOf() function
return op.countOf(inputString, " ")
# input string
inputString = "hello tutorialspoint python"
# calling the above defined countSpaces() function by
# passing input string as an argument
print("Count of no of spaces in an input string:", countSpaces(inputString))
Output
On executing, the above program will generate the following output -
Count of no of spaces in an input string: 2
Conclusion
In this article, we covered 5 different methods for counting the number of spaces in a string. Using the new operator function countOf, we learned how to count an element from any iterable. We also learned how to count the frequencies of each element of the iterable using dictionary hashing.