App*_*e27 3 python string character
我试图找出如何在字符串中添加字符的副本,只要字符是元音.例如,如果我输入单词copy('app'),理想情况下它将返回'aaaapp!'.我知道字符串是不可变的,但必须有一种方法!我一直盯着这几个小时.
注意:我不想要我的代码解决方案,最好只是提示让我朝着正确的方向前进.编辑:感谢您的帮助!
我的一个想法是:单词+ =单词+ i*4,但返回的内容类似'appaaaa!'
def copy(word):
"('string') ==> ('string') Adds four copies of vowel and an '!' to the string"
vowel = 'aeiouAEIOU'
for i in word:
if i in vowel:
#Missing code Here
return word + '!'
Run Code Online (Sandbox Code Playgroud)
你可以re.sub
很容易地使用:
>>> re.sub('([aeiouAEIOU])',r'\1\1\1\1','string')
'striiiing'
Run Code Online (Sandbox Code Playgroud)
或者,如果您希望替换数量可变:
>>> N=4
>>> re.sub('([aeiouAEIOU])',r'\1'*N,'string')
'striiiing'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3083 次 |
最近记录: |