Following the same idea as the one explained by Patricio in his answer, a somewhat more efficient way, although it will not really be noticed in small chains like yours, is to use string.translate
to eliminate the excluded characters of the string:
print(len(s.translate({ord(c): None for c in 'atgc'})))
However, if as you imply, you require the number of times that 'a', 'g', 't' and 'c' also appear, the most efficient by far is to use collections.Counter
instead of applying str.count
four times on the string. Even more so if what you are trying to tell are bases of a genetic chain where most of the characters are expected to be A, T, G and C:
from collections import Counter
s = 'acaaaaattgggaaacccccbvbm2xyyuuooopp5585'
c = Counter(s)
print("'a' aparece {} veces.".format(c["a"]))
print("'t' aparece {} veces.".format(c["t"]))
print("'g' aparece {} veces.".format(c["g"]))
print("'c' aparece {} veces.".format(c["c"]))
print("Caracteres diferentes a 'a', 'g', 't' y 'c' aparecen {} veces.".format(len(s) - c["a"] - c["g"] -c["t"] -c["c"]))
Exit:
'a' appears 9 times.
't' appears 2 times.
'g' appears 3 times.
'c' appears 6 times.
Characters other than 'a', 't', 'g' and 'c' appear 19 times.