How to count the number occurrences of specific character in a string with Python

3 Answers

0 votes
letters = "abacddefabffegga"

n = letters.count('a')

print(n)

'''
run:

4
  
'''

 



answered Aug 29, 2018 by avibootz
0 votes
from collections import Counter

letters = "abacddefabffegga"

counter = Counter(letters)

print(counter['a'])

'''
run:

4
  
'''

 



answered Aug 29, 2018 by avibootz
0 votes
import re

letters = "abacddefabffegga"

n = len(re.findall("a", letters))

print(n)

'''
run:

4
  
'''

 



answered Aug 29, 2018 by avibootz
...