Python program to count the number of spaces in string Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, there are various ways to Count the number of spaces in a String.Using count() Methodcount() 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) Output5 ExplanationThe count(" ") method counts how many spaces (" ") are present in the s, 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 ContentUsing for Loop and Conditional Check Using split() Method Using re.findall() with Regular ExpressionsUsing for LoopUsing 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) Output5 ExplanationThe 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) Output5 Explanationsplit() method divides the s 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) Output5 ExplanationThe re.findall(" ", s) function searches for all occurrences of spaces in the s 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. Comment More infoAdvertise with us Next Article Python Program to Count the Number of Vowels in a String A abhijithoyur Follow Improve Article Tags : Technical Scripter Python Python Programs Technical Scripter 2020 Python string-programs +1 More Practice Tags : python Similar Reads Python Program to Count the Number of Vowels in a String In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des 10 min read Python program to count the number of blank spaces in a text file All programs need input to process and after processing gives the output. Python support file handling and allow user to handle files. The concept of file handling has stretched over various other languages, But the implementation is either lengthy or complicated. Python treats file differently as t 3 min read Python Program to get number of consecutive repeated substring Given a substring K, the task is to write a Python Program to find the repetition of K string in each consecutive occurrence of K. Example Input : test_str = 'geeksgeeks are geeksgeeksgeeks for all geeks', K = "geeks" Output : [2, 3, 1] Explanation : First consecution of 'geeks' is 2. Input : test_s 4 min read Python program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou 3 min read Python - Ways to Count Number of Substring in String Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho 2 min read Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is 2 min read Like