[Tutor] string iteration

Glen Wheeler wheelege@tsn.cc
Sat, 10 Mar 2001 15:00:52 +1100


  Well, here is my typically newbie attempt...

length = 'abcdefghijklmnop'
data = 'abc'
resultant = ''
position = -1 ## because first spot is 0
for x in length:
    if position < (len(data) - 1):  ## len('abc') is 3, not 2.  so if the -1
was not there, it would try to iterate 4 times (0,1,2,3)
        position += 1
    else:
        position -= (len(data) - 1)
    resultant = resultant + data[position]
print resultant

  As you can see it's alot bigger and clunkier than Doug's attempt, but I
thought it best to give another solutional - the bigger and clunkier
something is the easier it is to understand (most of the time :).

  Glen.



----- Original Message -----
From: <ewe2@can.org.au>
To: <tutor@python.org>
Sent: Saturday, March 10, 2001 12:38 PM
Subject: [Tutor] string iteration


> ok, i'm feeling particularly stupid today:
>
> given a string a = 'abcdefghijklmnop' and a string b = 'abc', how do i
> iterate b against a, so i get a string c = 'abcabcabcabcabca' the length
of a?
> my brain is hurting from all the failed for loops :)
>
>
> --
> Sean Dwyer <ewe2@cvis.com.au>
> CAN Admin <ewe2@can.org.au>
> Web: http://ewe2.cvis.com.au/
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor