0

I have a problem with dividing a string for n length strings which should be in array. Is there any built function in Python which can do that, like for example .grouped in Scala?

e.g. word= 'ABCDEFG', n=3

Result: ['ABC','DEF','G']

sunny
  • 65
  • 1
  • 8
  • 1
    Does this answer your question? [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Tranbi Oct 07 '21 at 12:12

1 Answers1

0

This can be implemented using simple slicing.

n = 3
word = "ABCDEFG"

result = [word[i:i+n] for i in range(0, len(word), n)]

Also worth mentioning the chunked function from the more_itertools package.

Alex
  • 509
  • 4
  • 8