"Emil" <[EMAIL PROTECTED]> wrote

> I want to be capable of converting a string into a list where all 
> the items,
>  in  the list, have a fixed length not equal to 1

You can use list slicing to do this.
Combine with the stepsize parameter of the range function

> k = 'abcdefgh' and I want the fixed length for all the the
> items to be 2 then the list would look like ['ab', 'cd', 'ef, 'gh'].

k = 'abcdefgh'
new = []
for n in range(0,len(k),2):  # 2=stepsize
     new.append(k[n:n+2])   # use slice
print new

As a list comprehension that would be:

new = [k[n:n+2] for n in range(0,len(k),2)]

And as a more general function:

def splitStringByN(s,n):
   return [s[m:m+n] for m in range(0,len(s),n)]

Should work,

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to