-6

So I've got a string e.g "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN".

I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters and print that. Until there isn't 16 characters left.

Any help on how i'd do this?

ChrisB
  • 123
  • 5
  • 13

5 Answers5

3

Something like this?

string = "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN"
for n in range(len(string)-15):
    print(string[n:n+16])

You have to iterate over every character up to the last character that has 16 characters after it (so the length of the string, minus 15 (because indexing starts at 0) : len(string)-15), and then print the string sliced at that starting index up to the index + 16 (string[n:n+16]).

Slicing is an important and IMO powerful aspect of Python programming, it's a great read if you're new to the language (or programming in general) and you should definitely practice it. The official docs have some good information on the topic.

lennyklb
  • 1,307
  • 2
  • 15
  • 32
1

If I understood well, you might want to do something like:

a = "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN"

for i in range(len(a) - 15):
    print(a[i:i+16])
kecalace
  • 117
  • 1
  • 16
0

I would do:

str = "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN"

for i in range(len(str)):
    if len(str[i:i+16]) == 16:
        print(str[i:i+16])
    else: 
        break
Glrs
  • 1,060
  • 15
  • 26
  • This probably works but it involves an unnecessary if-else-condition which would be inefficient for very long strings (unnecessary iteration over excess characters and comparing string length every iteration), you don't need to iterate over the entire string and check if the length is valid afterwards. You can imply a correct string length by iterating over a subset of the characters (see the other answers) – lennyklb Apr 25 '17 at 08:48
  • True. I could also save the string in a variable so I don't access it twice. I just tried to make it self explanatory, but it's not something difficult anyway... oh well! – Glrs Apr 25 '17 at 08:54
  • The if statement may be replaced by a try/except loop that is more efficient in Python. – funk Apr 25 '17 at 09:25
  • @funk it actually doesn't pop up any exceptions, I just tried it. It just prints the string down to one character. In C I would expect a NullPointerException, but this doesn't seem to be the case with Python. – Glrs Apr 25 '17 at 09:30
  • I found an answer to that here http://stackoverflow.com/questions/9490058/why-substring-slicing-index-out-of-range-works-in-python – Glrs Apr 25 '17 at 09:35
  • @Tasos have to store the substring (let's say t = str[i:i+16] ) to a new variable and then try to access the 16th element of that string (t[15]). When the substring is shorter than 16 elements an exception will be raised. – funk Apr 25 '17 at 09:40
  • @Tasos Instead of using a new t variable, you can just access the 16th element using the str[i:i+16][15] syntax as mentioned at the question you quoted above. – funk Apr 25 '17 at 09:57
0

How about a list comprehension?

s = 'AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN'
print [s[x:x+16] for x in range(len(s)-15)]

['AABBCCCASSDSFGDF', 'ABBCCCASSDSFGDFG', 'BBCCCASSDSFGDFGH', 'BCCCASSDSFGDFGHD', 'CCCASSDSFGDFGHDG', 'CCASSDSFGDFGHDGH', 'CASSDSFGDFGHDGHR', 'ASSDSFGDFGHDGHRT', 'SSDSFGDFGHDGHRTF', 'SDSFGDFGHDGHRTFB', 'DSFGDFGHDGHRTFBF', 'SFGDFGHDGHRTFBFI', 'FGDFGHDGHRTFBFID', 'GDFGHDGHRTFBFIDH', 'DFGHDGHRTFBFIDHF', 'FGHDGHRTFBFIDHFD', 'GHDGHRTFBFIDHFDU', 'HDGHRTFBFIDHFDUF', 'DGHRTFBFIDHFDUFG', 'GHRTFBFIDHFDUFGH', 'HRTFBFIDHFDUFGHS', 'RTFBFIDHFDUFGHSI', 'TFBFIDHFDUFGHSIF', 'FBFIDHFDUFGHSIFU', 'BFIDHFDUFGHSIFUG', 'FIDHFDUFGHSIFUGE', 'IDHFDUFGHSIFUGEG', 'DHFDUFGHSIFUGEGF', 'HFDUFGHSIFUGEGFG', 'FDUFGHSIFUGEGFGN', 'DUFGHSIFUGEGFGNO', 'UFGHSIFUGEGFGNOD', 'FGHSIFUGEGFGNODN']
alex
  • 10,900
  • 15
  • 70
  • 100
0

For my case, I used the split method in python

# a string from the user e.g "Good Morning How are you ?"
text = input() 

# split string
split_text = text.split()  # to get the string as a list

print(split_text)

Result: ['Good', 'Morning', 'How', 'are', 'you', '?']

Neuron
  • 5,141
  • 5
  • 38
  • 59