[Tutor] Splitting long string into same len parts

Victor Bouffier victor at grupocdm.com
Sat Feb 11 04:49:10 CET 2006


On Thu, 2006-02-09 at 09:45 +0000, Alan Gauld wrote:

> 
> Define easier :-)
> 
Right!

> You could just use string slicing and a stepsize of 3 in range:
> 

> lst = [mystring[index : index+3] for index in range(0,len(mystring),3)]
> 
Ever since I found them, list comprehensions are my favorites.

> ... and you still have problems where the 
> string is not exactly divisible by 3, should you add padding?
> 

Alan, I understand where you are coming from. However, in this case I
don't have a problem since the incoming string will always be divisible
by 3, in this particular case. Thanks for pointing it out though.

Victor



More information about the Tutor mailing list