如何访问函数内部的字符串方法?



我有一个程序要编码。我想编写一个函数,它接受一个单词和一个字母。然后,该函数将删除word中出现的所有字母并返回它。

我这样编码:-

str = "Sushant"
def removeLetter(word, letter):
for j in range(0, len(word) + 1):
if j == letter:
word.replace(j, "")
return word
print(removeLetter(str, "s"))

但是它给了我空白。如何解决这个问题?

您不需要for循环(因为replace删除了word中所有字母的实例):

word = "Sushant"
def removeLetter(word, letter):
return word.replace(letter, '')
removeLetter(word, "s")

输出:

'Suhant'