-2

I want to know what is the best way of converting the string into list of string

For Example:

__string = 'ABCDEFGHIJ'
list_string = ['AB', 'CD', 'EF', 'GH', 'IJ']

How to do this?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

3
[a+b for a, b in (zip(*([iter(text)]*2)))]

# or

[text[i*2:i*2+2] for i in range(len(text)//2)]

# or
import re
re.findall("..?", text) //handles string of odd length

if the string is not even the less char will be missed. can be easily fixed if needed

shb
  • 5,957
  • 2
  • 15
  • 32
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
0

easy peasy xD

txt= 'ABCDEFGHIJ'

result = [txt[i:i+2] for i in range(0,len(txt),2)]
Ahmed4end
  • 274
  • 1
  • 5
  • 19