1

(New to Python after years of R use)

Say I have a string:

dna = "gctacccgtaatacgtttttttttt"

And I want to pre-define the indices of interest:

window_1 = 0:3
window_2 = 1:4
window_3 = 2:5

This is invalid python syntax. So I tried:

window_1 = range(0, 3)

This does not work when I try to use the window_1 variable as a string index:

dna[window_1]

But I get "string indices must be integers" error.

I have tried numerous things such as wrapping range() in int() and / or list() but nothing works.

Thanks

Luther_Blissett
  • 327
  • 1
  • 6
  • 16
  • 1
    Possible duplicate of [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – cody Feb 28 '19 at 17:32

1 Answers1

0

When you're setting your variables window_1, window_2, and window_3, first you have to tell it where you want it to look to grab these indices you're telling it to grab, so you need to tell it to look in your variable 'dna'. Secondly, the indices should be in square brackets. Also keep in mind that Python uses a zero based numbering system. So, the first position in your dna sequence(g) as far as Python is concerned is the zero position. The second position (c) is actually the number 1 position.

dna = "gctacccgtaatacgtttttttttt"

window_1 = dna[0:3] window_2 = dna[1:4] window_3 = dna[2:5]