Split a string by length

Yermat loic at fejoz.net
Thu Mar 25 11:50:01 CET 2004


Peter Otten a écrit :
> Pascal wrote:
> 
> 
>>How can I do to simulate this way:
>>'aabbcc'.split(2) -> ['aa', 'bb', 'cc']
>>I tried with a 'slice' but this didn't run:
>>[item for item in 'aabbcc'[::2]] -> ['a', 'b', 'c']
> 
> [...]

 >>> import re
 >>> re.findall(".{2}","aabbccdd")
['aa', 'bb', 'cc', 'dd']
 >>> re.findall(".{2}","aabbccdde")
['aa', 'bb', 'cc', 'dd']

Yermat




More information about the Python-list mailing list