Q:

Splitting strings in python based on index

This sounds pretty basic but I ca't think of a neat straightforward method to do this in Python yet
I have a string like "abcdefgh" and I need to create a list of elements picking two characters at a time from the string to get ['ab','cd','ef','gh'].
What I am doing right now is this
output = []

for i in range(0,len(input),2):
  output.append(input[i:i+2])

Is there a nicer way?

A:

In [2]: s = 'abcdefgh'

In [3]: [s[i:i+2] for i in range(0, len(s), 2)]
Out[3]: ['ab', 'cd', 'ef', 'gh']

