Counting adenine and cytosine bases I've started a little challenge on a website, and the first one was about counting different DNA letters. I've done it, but I found my method very brutal. I have a little experience, and I know that hard-coded information is not good. Any way to improve this code?

chaine = "AAAACCCGGT"

nA = 0
nT = 0
nG = 0
nC = 0

for i in range(len(chaine)):
   if chaine[i] == "A":
       nA = nA + 1
   if chaine[i] == "C":
       nC = nC + 1
print nA, nC
for i in range(len(...)): is almost never the right way to go in Python. Instead, in this case, you should iterate directly over the characters:

for char in chaine:
    if char == 'A':
        ...




Rather than separate names nA, nC, etc., use a dictionary to map each character to its count:

counts = {'A': 0, 'C': 0, ...}


Now you don't need the ifs at all:

for char in chaine:
    counts[char] += 1
print counts['A'], counts['C']




In fact, Python already implements collections.Counter, a dictionary subclass that makes this task trivial:

from collections import Counter

counts = Counter(chaine)
print counts['A'], counts['C']