python split word into letter pairs 1

python split word into letter pairs

string = 'ABCDXY'
[string[i:i+2] for i in xrange(0, len(string), 2)]

Here is what the above code is Doing:
1. xrange(0, len(string), 2) is a list of numbers from 0 to len(string) with a step of 2.
2. [string[i:i+2] for i in xrange(0, len(string), 2)] is a list comprehension that iterates over the list of numbers from 0 to len(string) with a step of 2.
3. string[i:i+2] is a slice of string from i to i+2.

Similar Posts