```
def countSegments(self, s):
res = 0
for i in range(len(s)):
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
res += 1
return res
```

or one line:

```
def countSegments(self, s):
return sum([s[i] != ' ' and (i == 0 or s[i - 1] == ' ') for i in range(len(s))])
```