这是我到目前为止所拥有的,但为了替换 a、b 和 c,我想知道如何合并替换句子中所有出现的所有 3 个字母。我也不允许使用replace()函数。
def changeLetters(word):
for letter in word:
if letter == "a": #I would like to replace a,b and c
word.replace(letter,"!") #replace the replace() function
return word
用户输入示例:
Amy buys carrots and apples
用户输出示例:
!my 3uys 8!rrots !nd !pples
最佳答案
word = 'Amy buys carrots and apples'
result = ''.join(['!' if x == 'a' else '3' if x == 'b' else '8' if x == 'c' else x for x in word.lower()])
result
'!my 3uys 8!rrots !nd !pples'
关于python - 如何在Python中不使用replace()函数的情况下替换字符串中句子中所有实例中的字母(例如ABC)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58886620/