Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 26, 2018 21:42
Show Gist options
  • Save bbookman/f0d36dfee6f6b3aa861fb026f1d5eb83 to your computer and use it in GitHub Desktop.
Save bbookman/f0d36dfee6f6b3aa861fb026f1d5eb83 to your computer and use it in GitHub Desktop.
Python List Comprehension: Count the number of spaces in a string
'''
Count the number of spaces in a string
'''
some_string = 'the slow solid squid swam sumptuously through the slimy swamp'
spaces = [s for s in some_string if s == ' ']
print(len(spaces))
@RudraPatel5435
Copy link

l = some_string.count(" ")
print(l)

@RudraPatel5435
Copy link

l = some_string.split()
print(len(l) - 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment