0

I am trying to remove part of a string withing a string. for example if a string is "atgtga" i want the output to be "atg". i tried using the .replace("tga","") method but my TA was saying that it only masks that part of the string instead of removing it.

i tried a new approach but i am still stuck on the if statement.

x="atgtgaacttaa"
c=0
q=3

while(q<=len(x)):
    x=x[c:q]

    if(x=="tga" or x=="taa" or x=="tag"):

       c=c+3
       q=q+3

print(x)

I tried making a new function but my output is ignoring "atg" for some reason and deleting it.

 def get_orf(dna_seq):
   x=0

   while(x<=len(dna_seq)):
     if("taa" in dna_seq or "tag" in dna_seq or "tga" in dna_seq ):
       dna_seq=dna_seq.replace("taa","") 
       dna_seq=dna_seq.replace("tag","") 
       dna_seq=dna_seq.replace("tga","")
       return dna_seq
     else:
      return dna_seq
     x=x+1
fivestar
  • 17
  • 2

2 Answers2

2

You can use replace, sometimes things do what they say on the tin:

>>> x = "atgtga"
>>> y = x.replace("tga", "")
>>> print(y)
atg
donkopotamus
  • 22,114
  • 2
  • 48
  • 60
0

Your TA is incorrect. replace returns a new string with the indicated change. The original string is, indeed, still intact, but the new string is a separate entity, not part of the original, and not dependent on the original any more.

Prune
  • 76,765
  • 14
  • 60
  • 81