Iterate over a string 2 (or n) characters at a time in Python

I don't know about cleaner, but there's another alternative:

for (op, code) in zip(s[0::2], s[1::2]):
    print op, code

A no-copy version:

from itertools import izip, islice
for (op, code) in izip(islice(s, 0, None, 2), islice(s, 1, None, 2)):
    print op, code

Triptych inspired this more general solution:

def slicen(s, n, truncate=False):
    assert n > 0
    while len(s) >= n:
        yield s[:n]
        s = s[n:]
    if len(s) and not truncate:
        yield s

for op, code in slicen("+c-R+D-e", 2):
    print op,code

Maybe this would be cleaner?

s = "+c-R+D-e"
for i in xrange(0, len(s), 2):
    op, code = s[i:i+2]
    print op, code

You could perhaps write a generator to do what you want, maybe that would be more pythonic :)