Count number of occurrences of a I have in string Python

Method 1 -

s = "a wedef aa c"
s.count('a')

Method 2 -

s = "a wedef aa c"
sum([1 for _ in s if _ == 'a' ])

Method 3 -

s = "a wedef aa c"
c = 0
for i in s:
if i=='a':
c+=1
print c

Execution time test

import timeit

def count_method_1():
s = "a wedef aa c"
return s.count('a')

def count_method_2():
s = "a wedef aa c"
return sum([1 for _ in s if _ == 'a'])

def count_method_3():
s = "a wedef aa c"
c = 0
for i in s:
if i == 'a':
c += 1
return c

def main():
time = timeit.repeat(lambda: count_method_1())
print("Method 1 time - ", time)
time = timeit.repeat(lambda: count_method_2())
print("Method 2 time - ", time)
time = timeit.repeat(lambda: count_method_3())
print("Method 3 time - ", time)

if __name__ == "__main__":
main()


Result

Method 1 time -  [0.3184861, 0.29942139999999995, 0.28848870000000004, 0.28497740000000016, 0.29332040000000004]
Method 2 time -  [0.8437245, 0.8938065000000002, 0.8882654999999997, 0.9092678000000003, 0.8961607000000003]
Method 3 time -  [0.6849796000000001, 0.6760326000000001, 0.6770035000000005, 0.7099235999999998, 0.6783089999999987]

Method 1 (s.count('a')) is the fastest.