[Tutor] Splitting long string into same len parts
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Feb 9 03:55:48 CET 2006
On Wed, 8 Feb 2006, Victor Bouffier wrote:
> Hi to all,
>
> I'd like to split a long string into equally long strings (len(str) =
> 3). I did the following using regexes:
>
> >>> n = 'xb1jyzqnd1eenkokqnhep6vp692qi9tmag3owzqw0sdq3zjf'
> >>> o = re.split(r'(...)', n)
> >>> print o
> ['', 'xb1', '', 'jyz', '', 'qnd', '', '1ee', '', 'nko', '', 'kqn', '',
> 'hep', '', '6vp', '', '692', '', 'qi9', '', 'tma', '', 'g3o', '', 'wzq',
> '', 'w0s', '', 'dq3', '', 'zjf', '']
>
> Which gives me empty strings between each value.
Hi Victor,
Try using re.findall() instead of re.split(). The behavior you're seeing
with split is perfectly logical: each pair of empty strings is being split
by that three-character sequence.
Best of wishes!
More information about the Tutor
mailing list