Soham Sharma
Soham Sharma

Reputation: 31

How to slice a string with different starting and end points in Python

I have a string like rna = "UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGCUAA" and I want to iterate through the string and capture the different strings which start with 'AUG' and with 'UAA' or 'UAG' or 'UGA'.

This is the code I've written so far:

rna = "UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGCUAA"      # start --> AUG; STOP --> UAA, UAG, UGA
hello = " "
n = 3
list = []
for i in range(0, len(rna), n):
    list.append(rna[i:i+n])

for i in list:
    if i == "AUG":
        hello += i
        i = i + 1
        if i != "UAA" or "UAG" or "UGA":
            hello += i

This is giving me a type error:- error

Upvotes: 0

Views: 99

Answers (1)

Oghli
Oghli

Reputation: 2340

The problem in this line of code:

if i != "UAA" or "UAG" or "UGA":
            hello += i

You should check each value of object i alone:

if i!= "UAA" or i!= "UAG" or i!= "UGA":
            hello += i

Or simply you can check for the condition:

 if i not in ["UAA", "UAG", "UGA"]:
           hello += i

Also you can't concatenate string value with int value in this line of code:

i = i + 1

You should cast 1 to string data type.

Upvotes: 1

Related Questions