<p>在问这个问题之前,我已经检查过我的问题是否有解决办法。在</p>
<p>我在做Python。我正试图编写一个输入字符串的代码,对于该字符串中的每个字母(元音(<code>aeiou</code>),将该元音本身替换4次。然后在末尾添加一个感叹号(<code>!</code>)。示例:</p>
<p><code>apple</code>看起来像<code>aaaappleeee!</code>。在</p>
<p>我试了很多次都没有效果。不知道你们能不能看看我的代码看看我做错了什么。拥抱和亲吻。克里斯托弗</p>
<pre><code>def exclamation (string):
for i in range(0,len(string)):
if string[i] in 'aeiou':
string.replace(string[i],string[i*4])
print(string + '!')
</code></pre>
<p>您可以迭代一个字符串,这样就不必修改范围,<code>str.replace</code>也不会修改现有的字符串。总之,你可以这样做:</p>
<pre><code>def exclamation(string):
for c in "aeiou":
string = string.replace(c, c*4)
print(string + "!")
</code></pre>