The problem is that you replace C
with G
then G
with C
. One simple way to prevent you going from one to the other would be to replace C
with g
, so it wouldn't then go back to C
, then uppercase the result:
gattaca="GATTACA"
rev = gattaca[::-1]
print rev.replace('A','u').replace('T','a').replace('C','g').replace('G','c').upper()
This correctly outputs UGUAAUC
instead of UCUAAUC
as your example did.
UPDATE
The more Pythonic way, though, and avoiding the case-based hack, and being more efficient as the string doesn't need to be scanned five times, and being more obvious as to the purpose, would be:
from string import maketrans
transtab = maketrans("ATCG", "UAGC")
print rev.translate(transtab)