我想用字符串删除字符串的一部分。例如,如果一个字符串是“atgtga”,我希望输出是“atg”。 我尝试使用.replace(“tga”,“”)方法,但我的TA说它只是掩盖了字符串的一部分而不是删除它。从字符串中删除字符串
我尝试了一种新方法,但我仍然坚持if语句。
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)
我试图使一个新的功能,但我的输出被忽略出于某种原因“ATG”和删除它。
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
你的助教是错的。 – Shadow