擅长:python、mysql、java
<p>变更如下:</p>
<pre><code>def replace(string, word):
new, pos = '', 0
for c in string:
if c.isalpha():
new += word[pos%len(word)] # rotate through replacement string
pos += 1 # increment position in current word
else:
new += c
pos = 0 # reset position in current word
return new
>>> replace('my name is greg', 'hi')
'hi hihi hi hihi'
</code></pre>