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
1
  • 3
    Your TA is wrong. Commented Oct 19, 2017 at 22:19

2 Answers 2

2

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

>>> x = "atgtga"
>>> y = x.replace("tga", "")
>>> print(y)
atg
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.