If you want to access a string 3 characters at a time, you will need slicing .
You can get a list of 3-character long snippets of a string using the following list comprehension:
>>> x = 'this is a string' >>> step = 3 >>> [x[i:i+step] for i in range(0, len(x), step)] ['thi', i', a', ' st', 'rin', 'g'] >>> step = 5 >>> [x[i:i+step] for i in range(0, len(x), step)] ['this ', 'is a ', 'strin', 'g']
Important bit:
[x[i:i+step] for i in range(0, len(x), step)]
range(0, len(x), step) gets the start indices of each fragment step -character. for i in will iterate over these indices. x[i:i+step] gets a slice x starting with index i and length step .
If you know that each time you get exactly four pieces, then you can do:
a, b, c, d = [x[i:i+step] for i in range(0, len(x), step)]
This will happen if 3 * step < len(x) <= 4 * step .
If you don't have exactly four parts, then Python will give you a ValueError , trying to unpack this list. Because of this, I would find this method very fragile and not use it.
You can just do
x_pieces = [x[i:i+step] for i in range(0, len(x), step)]
Now that you used to access a , you can access x_pieces[0] . For b you can use x_pieces[1] and so on. This allows you to greatly increase flexibility.