In Python strip () function does not work

strip (Python in) can remove both ends of the "blank"

If you develop a char, then str.strip (char) can eliminate both ends of the character char included!

But if you take out the operation after operation, there is no work occurs, is not endowed with a new value:

    def __str__(self):
        msg = self.cookedString + '地瓜'
        if self.condiments:
            msg += "佐料有("
            for condiment in self.condiments:
                msg += condiment + ","
            msg.strip(",")    #出错之处!
            msg += ")"
        return msg

The correct wording is as follows:

    def __str__(self):
        msg = self.cookedString + '地瓜'
        if self.condiments:
            msg += "佐料有("
            for condiment in self.condiments:
                msg += condiment + ","
            msg = msg.strip(",")    #出错之处!
            msg += ")"
        return msg

For the following reasons:

      String in Python is immutable type, you do strip itself of msg () is the value of the original can not be changed, but the strip () returns a new value needs to be saved again before they can reflect!

Guess you like

Origin blog.csdn.net/qq_19672707/article/details/79573715