Split string every nth character?


Question

Is it possible to split a python string every nth character?

For example, suppose I have a string containing the following:

'1234567890'

How can I get it to look like this:

['12','34','56','78','90']
1
325
7/14/2018 12:26:58 PM

>>> line = '1234567890'
>>> n = 2
>>> [line[i:i+n] for i in range(0, len(line), n)]
['12', '34', '56', '78', '90']
483
2/28/2012 2:02:36 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon