Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Count Spaces in String
00:00
5 left

Count Spaces in String

EasyPython

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):
Interviewer

Your question is Count Spaces in String. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.