[sapug] Simple Sequence Question

Chris Were chris.were at gmail.com
Tue Jun 20 11:06:18 CEST 2006


Or you could do the following if you don't know how many characters
the string will have:

t = ""
while len(s) > 0:
    t += s[:4]+"."
    s = s[4:]
t = t[:-1]

> > Is there a simple slicing operation? Or do I need to do something like:
> >
> > t = ''
> > for i in xrange(len(s)):
> >   if not i == 0 and i % 4 == 0:
> >     t += ':'
> >   t += s[i]
>
> Well, it's looping and slicing:
>
> s = '1234567812345678'
> t, s = s[:4], s[4:]
> while s:
>     t, s = t + ':' + s[:4], s[4:]

-- 

http://www.chriswere.com/


More information about the sapug mailing list