Python strip() not working inside a function

ε祈祈猫儿з 提交于 2019-12-02 13:44:01

strip is not an in-place method, meaning it returns a value which must be reassigned like so:

str1 = str1.strip() # the string is reassigned to the returned stripped string

Three things I see.

First, you are not assigning the strip variable to anything, second you are trying to do this in a return.

The return should only have the variables you wish to return to be used in another function. You can print from this function, but your return statement should not have any activity, only the variable to be returned.

Third, that print statement looks off to me. At the very least it isn't how I would do the print.

def func(str1):
    str1 = "  abdced "

    str2 = str1.strip()

    print(str2)

    return str1, str2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!