Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Spaces in String

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Count Spaces in String. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Count the number of spaces in a string.

Implement count_spaces(s) to return the number of literal space characters (' '). Do not count tabs, newlines, or other whitespace characters.

Input and Output

  • Input: a string s.
  • Output: an integer containing the number of literal spaces.

Constraints

  • 0 <= len(s) <= 10^5
  • The string may contain any Unicode characters
  • Count only the ASCII space character ' '

Function Signature

def count_spaces(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output