Reputation: 3
I thought it would be easy to replace the string a into u and so on with this code
DNA= "atgcatgctagcagtcagcatcgatcgatcagctagctag"
def transcribe(dna):
dna.replace("a", "u")
dna.replace("t", "a")
dna.replace("g", "c")
dna.replace("c", "g")
return
it didn't change my variable at all. Can anybody help me to solve this?
Thank you
Upvotes: 0
Views: 116
Reputation: 198566
dna.replace
will yield the new string; it will not change dna
. You would need to assign the result to something.c
with g
and then g
with c
does not do what you think. E.g. atcg
-> atgg
-> atcc
.The solution is to replace simultaneously:
import string
def transcribe(dna):
return dna.translate(string.maketrans("atgc", "uacg"))
or to replace with an intermediary value:
def transcribe(dna):
dna = dna.replace("a", "u")
dna = dna.replace("t", "a")
dna = dna.replace('g', '_')
dna = dna.replace('c', 'g')
dna = dna.replace('_', 'c')
return dna
Upvotes: 2