Python program to count the number of spaces in string (original) (raw)
Last Updated : 10 Jan, 2025
In Python, there are various ways to Count the number of spaces in a String.
Using count()
Method
count() method in Python is used to return the number of occurrences of a specified element in a list or string
Python `
s = "Count the spaces in this string."
Count spaces using the count() method
space_count = s.count(" ")
print(space_count)
`
**Explanation
- The**
count(" ")
** method counts how many spaces (" "
) are present in thes
, which in this case is"Count the spaces in this string."
. - The result, stored in
space_count
, is printed, showing the total number of spaces found in the string.
Let’s understand various other methods to count the number of spaces in a string.
Table of Content
- Using for Loop and Conditional Check
- Using split() Method
- Using re.findall() with Regular Expressions
**Using for
Loop
Using a for loop with a conditional check allows us to iterate through a string or list and perform an action based on a specific condition, such as counting occurrences of a particular character
Python `
s = "Count the spaces in this string."
Initialize space counter
space_count = 0
Loop through each character in the string
for char in s: # Check if the character is a space if char == " ": # Increment space count space_count += 1
print(space_count)
`
**Explanation
- The for loop iterates through each character in the
s
, and the conditional check (if char == " "
) determines if the character is a space. - If a space is found, the
space_count
is incremented, and the total number of spaces is printed after the loop finishes.
**Using split()
split() method in Python splits a string into a list of substrings based on a specified delimiter, which can be used to count spaces by calculating the difference between the total number of words and one.
Python `
s = "Count the spaces in this string."
Split the string by spaces and count the spaces
space_count = len(s.split()) - 1
print(space_count)
`
**Explanation
split()
method divides thes
into a list of words by spaces, and len() calculates the length of this list, representing the total number of words.- By subtracting 1 from the word count, we get the number of spaces, since the number of spaces is always one less than the number of words.
**Using re.findall()
re.findall() function with regular expressions allows us to find all occurrences of a specific pattern, such as spaces, and count them by measuring the length of the resulting list.
Python `
Import the regular expression module
import re
Input string
s = "Count the spaces in this string."
Use re.findall() to find all spaces
space_count = len(re.findall(" ", s))
Print the number of spaces
print(space_count)
`
**Explanation
- The
re.findall(" ", s)
function searches for all occurrences of spaces in thes
and returns a list of all spaces found. - The
len()
function then calculates the number of spaces by measuring the length of this list, which is printed as the result.