[Tutor] string iteration
Doug Stanfield
DOUGS@oceanic.com
Fri, 9 Mar 2001 15:51:05 -1000
[Sean Dwyer asked:]
> 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 :)
One way:
Python 1.5.2 (#1, Apr 18 1999, 16:03:16) [GCC pgcc-2.91.60 19981201
(egcs-1.1.1 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> a = 'abcdefghijklmnop'
>>> b = 'abc'
>>> spam = b * int(len(a)/len(b))
>>> spam
'abcabcabcabcabc'
>>> spam = b * (int(len(a)/len(b))+1)
>>> spam
'abcabcabcabcabcabc'
>>> eggs = spam[:len(a)]
>>> eggs
'abcabcabcabcabca'
>>>
Does that help?
-Doug-