Skip to content

Instantly share code, notes, and snippets.

@Geekfish
Created May 15, 2012 16:12
Show Gist options
  • Save Geekfish/2702957 to your computer and use it in GitHub Desktop.
Save Geekfish/2702957 to your computer and use it in GitHub Desktop.
Split a string every x characters
def split_per_x_chars(m, step):
return [m[x:x+step] for x in range(0, len(m), step)]
split_per_x_chars('abcdefghijklmnop', 3)
# ['abc', 'def', 'ghi', 'jkl', 'mno', 'p']
@dionyziz
Copy link

Why not just?

def split_per_x_chars(m, step):
return [ m[x:x+step] for x in range(0, len(m), step) ]

@gtklocker
Copy link

@dionyziz That is correct, ''.join() is completely unneeded in this case.

@Geekfish
Copy link
Author

indeed, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment