12

I have a string

ddffjh3gs

I want to convert it into a list

["ddf", "fjh", "3gs"]

In groups of 3 characters as seen above. What is the best way to do this in python 2.7?

Qwerty
  • 1,252
  • 1
  • 11
  • 23

1 Answers1

27

Using list comprehension with string slice:

>>> s = 'ddffjh3gs'
>>> [s[i:i+3] for i in range(0, len(s), 3)]
['ddf', 'fjh', '3gs']
falsetru
  • 357,413
  • 63
  • 732
  • 636