Split a string to even sized chunks

后端 未结 8 2277
粉色の甜心
粉色の甜心 2020-11-27 23:05

How would I be able to take a string like \'aaaaaaaaaaaaaaaaaaaaaaa\' and split it into 4 length tuples like (aaaa,aaaa,aaaa

8条回答
  •  不知归路
    2020-11-27 23:19

    s = 'abcdef'
    

    We need to split in parts of 2

    [s[pos:pos+2] for pos,i in enumerate(list(s)) if pos%2 == 0]
    

    Answer:

    ['ab', 'cd', 'ef']
    

提交回复
热议问题