9

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,194
  • 1
  • 9
  • 21

1 Answers1

22

Using list comprehension with string slice:

>>> s = 'ddffjh3gs'
>>> [s[i:i+3] for i in range(0, len(s), 3)]
['ddf', 'fjh', '3gs']
falsetru
  • 336,967
  • 57
  • 673
  • 597