Python strip()在函数内部不起作用

Chr*_*eng -5 python string strip

我试图用来strip()修剪字符串前后的空格.它工作正常

str1 = "  abdced "
str1.strip()
Run Code Online (Sandbox Code Playgroud)

但是当我在函数中使用它时,它不起作用:

def func(str1):
    return str1.strip() 

print func("   abdwe ") 
Run Code Online (Sandbox Code Playgroud)

它不会削减任何空间.谁能说出发生了什么?谢谢!

Mal*_*imi 6

strip 不是就地方法,这意味着它返回一个必须重新分配的值,如下所示:

str1 = str1.strip() # the string is reassigned to the returned stripped string
Run Code Online (Sandbox Code Playgroud)