I've got the following exercise: Write the function countA(word) that takes in a word as argument and returns the number of 'a' in that word. Examples
>>> countA("apple")
1
>>> countA("Apple")
0
>>> countA("Banana")
3
My solution is:
def countA(word):
return len([1 for x in word if x is 'a'])
And it's ok. But I'm not sure if it's the best pythonic way to solve this, since I create a list of 1 values and check its length. Can anyone suggest something more pythonic?
word.count('a')?