Skip to content

Instantly share code, notes, and snippets.

@hidsh
Created August 25, 2013 00:18
Show Gist options
  • Save hidsh/6331143 to your computer and use it in GitHub Desktop.
Save hidsh/6331143 to your computer and use it in GitHub Desktop.
Python: 文字列を文字数で分割する
def split_str(s, n):
"split string by its length"
length = len(s)
return [s[i:i+n] for i in range(0, length, n)]
if __name__ == '__main__':
print(split_str('qwertyuiop', 2))
print(split_str('qwertyuiop', 3))
['qw', 'er', 'ty', 'ui', 'op']
['qwe', 'rty', 'uio', 'p']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment