2017-01-22 72 views
0

我想从一个字符串在Python 3下删除一个角色是我的代码:试图从一个字符串在Python删除一个3字

#Function that removes a character from a string 
def removeChar(character, string): 
    new_string = string.replace(character, "") 

print(removeChar("e", "Hello World")) 

不过,这一方案的输出只是None。我的代码有什么问题?

+2

,因为你没有返回'new_string'值... –

+1

加上'在函数的最后返回new_string' – rassar

回答

2

你有自己的功能如下后返回new_string

def removeChar(character, string): 
    new_string = string.replace(character, "") 
    return new_string 

print(removeChar("e", "Hello World")) 
2

那么如果一个函数没有return任何东西,Python解释器会让它返回None。所以,你应该声明:

def removeChar(character, string): 
    returnstring.replace(character, "")

而且你真的不从字符串中去掉一个字符,字符串是不变,您所创建的字符串,其中的字符缺失相比定字符串的一个副本。

相关问题