Create a generator function that yields smaller and smaller slices of the given string.
def shrink(s):
for i in range(len(s)):
yield s[i:]
Create a function that splits a string into a list of five character segments.
def split_into_five_character_segments(s):
ret = []
while len(s) > 5:
ret.append(s[:5])
s = s[5:]
ret.append(s)
return ret
Combine the two in a list comprehension to generate your fragment library.
sequence = "MSSPPPARSGFYRQEVTKTAWEVRAVYRDLQ"
fragments = [split_into_five_character_segments(s) for s in shrink(sequence)]
Enumerate through each fragment. Use join
to combine the pieces of the fragment into a single space-separated string.
for idx, fragment in enumerate(fragments):
fragment_number = idx + 1
indent = " " * idx
print indent + str(fragment_number)
print indent + " ".join(fragment)
Result:
1
MSSPP PARSG FYRQE VTKTA WEVRA VYRDL Q
2
SSPPP ARSGF YRQEV TKTAW EVRAV YRDLQ
3
SPPPA RSGFY RQEVT KTAWE VRAVY RDLQ
4
PPPAR SGFYR QEVTK TAWEV RAVYR DLQ
5
PPARS GFYRQ EVTKT AWEVR AVYRD LQ
6
PARSG FYRQE VTKTA WEVRA VYRDL Q
7
ARSGF YRQEV TKTAW EVRAV YRDLQ
8
RSGFY RQEVT KTAWE VRAVY RDLQ
9
SGFYR QEVTK TAWEV RAVYR DLQ
10
GFYRQ EVTKT AWEVR AVYRD LQ
11
FYRQE VTKTA WEVRA VYRDL Q
12
YRQEV TKTAW EVRAV YRDLQ
13
RQEVT KTAWE VRAVY RDLQ
14
QEVTK TAWEV RAVYR DLQ
15
EVTKT AWEVR AVYRD LQ
16
VTKTA WEVRA VYRDL Q
17
TKTAW EVRAV YRDLQ
18
KTAWE VRAVY RDLQ
19
TAWEV RAVYR DLQ
20
AWEVR AVYRD LQ
21
WEVRA VYRDL Q
22
EVRAV YRDLQ
23
VRAVY RDLQ
24
RAVYR DLQ
25
AVYRD LQ
26
VYRDL Q
27
YRDLQ
28
RDLQ
29
DLQ
30
LQ
31
Q