• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Count Spaces in String in Python

Count Spaces in String in Python

May 6, 2022 Leave a Comment

In Python, we can easily count how many spaces are in a string using a loop and counting the number of spaces we find in the string.

def countSpaces(string):
    count = 0
    for char in string:
        if char in " ":
           count = count + 1
    return count

print(countSpaces("Hello World!"))

#Output:
1

When working with strings, it can be useful to know how many spaces appear inside a variable.

In Python, we can easily get the count of spaces in a string looping over each character of the string and seeing if it is a space or not.

Below is a function which will count the number of spaces for you in a string using Python.

def countSpaces(string):
    count = 0
    for char in string:
        if char in " ":
           count = count + 1
    return count

print(countSpaces("Hello World!"))
print(countSpaces("This is a string with some words."))
print(countSpaces("What's up?"))

#Output:
1
6
1

Hopefully this article has been useful for you to learn how to count the number of vowels in a string using Python.

Other Articles You'll Also Like:

  • 1.  Keep Every Nth Element in List in Python
  • 2.  Using Python to Append Character to String Variable
  • 3.  Get pandas Column Names as List in Python
  • 4.  Using Python to Get Queue Size
  • 5.  Remove None From List Using Python
  • 6.  Get pandas Index Values as List in Python
  • 7.  Convert Integer to Bytes in Python
  • 8.  Using Python to Count Number of False in List
  • 9.  Truncate String in Python with String Slicing
  • 10.  Read Pickle Files with pandas read_pickle Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy