The problem with your code is that you always extract the substring from the index, which is divisible by 3. Try instead
a = 'AUGGCCAUA'
def getSubStrings(RNA, position):
return [RNA[i:i+3] for i in range(position, len(RNA) - 2, 3)]
print getSubStrings(a, 0)
print getSubStrings(a, 1)
print getSubStrings(a, 2)
Exit
['AUG', 'GCC', 'AUA']
['UGG', 'CCA']
['GGC', 'CAU']
Explanation
range(position, len(RNA) - 2, 3)will generate a list of numbers with a total difference of 3, starting from positionup to the length of the list - 2. For example,
print range(1, 8, 3)
1 - , 8 - , 3 - ,
[1, 4, 7]
. ,
[RNA[i:i+3] for i in range(position, len(RNA) - 2, 3)]