If i have this string:
"0110100001100101011011000110110001101111"
How can I divide it at every eighth character into smaller strings, and put it into a list, so that it looks like this?:
['01101000','01100101','01101100','01101100','01101111']
So far, I can't figure out how this would be possible.
I should mention, since my strings are in binary so the length is always a multiple of 8.
>>> mystr = "0110100001100101011011000110110001101111"
>>> [mystr[i:i+8] for i in range(0, len(mystr), 8)]
['01101000', '01100101', '01101100', '01101100', '01101111']
The solution uses a so called list comprehension (this seems to be a pretty decent tutorial) and is doing basically the same as Aleph's answer, just in one line.
t=[]
for i in range(len(yourstring)/8):
t.append(yourstring[i*8: (i+1)*8])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With