Created
December 26, 2018 21:42
-
-
Save bbookman/f0d36dfee6f6b3aa861fb026f1d5eb83 to your computer and use it in GitHub Desktop.
Python List Comprehension: Count the number of spaces in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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)) |
l=sum([1 for i in range(len(s))if s[i]==' '])
Is this still list comprehension without [i for i ...] format? But if the result we want to get is a list with a number of spaces in the string this code should be fine:
space_find = [some_string.count(' ')] print(space_find)
here you are not using list comprehension
the_number_of_white_Spaces_in_any_string = [input(str('Waiting for a sentence...')).count(' ')]
res = sum([1 for x in s if x == ' '])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this still list comprehension without [i for i ...] format?
But if the result we want to get is a list with a number of spaces in the string this code should be fine:
space_find = [some_string.count(' ')]
print(space_find)