Python strip() not working inside a function

后端 未结 2 1265
一向
一向 2021-01-29 15:44

I am trying to use strip() to trim off the space before and after a string. It works fine for

str1 = \"  abdced \"
str1.strip()

H

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 16:24

    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
    

提交回复
热议问题