在Python中,`strip()`方法用于删除字符串开头和结尾的指定字符(默认为空格字符)。以下是`strip()`方法的基本用法:
删除字符串开头和结尾的空格字符string = " hello world "result = string.strip()print(result) 输出:"hello world"删除字符串开头和结尾的指定字符string = "!!!hello world!!!"result = string.strip("!")print(result) 输出:"hello world"删除字符串开头和结尾的多个指定字符string = "!!!hello world!!!"result = string.strip("!")print(result) 输出:"hello world"
`strip()`方法返回一个新的字符串,原始字符串不会被修改。如果不指定参数,默认删除开头和结尾的空格字符。

另外,还有`lstrip()`和`rstrip()`方法,分别用于删除字符串开头和结尾的字符,以及字符串开头和结尾的字符,但不包括中间的部分:
删除字符串开头和结尾的空白字符string = " \n Hello World! \n "result = string.strip()print(result) 输出:"Hello World!"删除字符串开头和结尾的指定字符string = "*Hello World!!!*"result = string.strip("*")print(result) 输出:"Hello World"
需要注意的是,`strip()`, `lstrip()`, 和 `rstrip()` 方法都不会修改原始字符串,而是返回一个新的字符串
