-1

I'm trying to know how many spaces in list for words

list = ["Hello       Hello Hello    Hello"]

How can I do it?

list = ["Hello       Hello Hello    Hello"]

def readSpace():
    print(list.space())

I'm trying like this :

thx.

JoErNanO
  • 2,458
  • 1
  • 25
  • 27
davai
  • 3
  • 2

2 Answers2

2

You can use string.count method to count the number of occurrences of a character in a string:

>>> lst = ["Hello       Hello Hello    Hello", "exa   mple"]
>>> [x.count(" ") for x in lst]
[12, 3]

So, you can modify your method to:

def count_spaces(lst):
    return [x.count(" ") for x in lst]

Note that using list as variable name is a poor choice of variable name because it clashes with built-in list, and so you should avoid using it.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
1

You can do this with a list comprehension and the count() string method . Something like:

>>> list = ["Hello       Hello Hello    Hello"]
>>> [x.count(" ") for x in list]
 [12]
JoErNanO
  • 2,458
  • 1
  • 25
  • 27