
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
- Related Articles
- Python program to count number of substring present in string
- Python Program to Count Number of Lowercase Characters in a String
- C Program to count vowels, digits, spaces, consonants using the string concepts
- Count Number of Lowercase Characters in a String in Python Program
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Python program to move spaces to front of string in single traversal
- Python program to count number of vowels using set in a given string
- Java Program to count the number of words in a String
- C# program to count the number of words in a string
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- Program to count number of distinct characters of every substring of a string in Python
- Python Program to Replace the Spaces of a String with a Specific Character
- Program to count the number of consistent strings in Python
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
