python中字符串的strip(),lstrip(),rstrip()的含义

 

string.lstrip(s[, chars])

Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

string.rstrip(s[, chars])

Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

string.strip(s[, chars])

Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, charsmust be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.


 

1

2

3

4

demoStr.lstrip() = 去除left左边的白空格  = "hello wold !    "

demoStr.rstrip() = 去除right右边的白空格 = "      hello wold !"

demoStr.strip() = demoStr.lstrip().rstrip()=去除left左边和right右边=去除首尾的白空格="hello wold !"


猜你喜欢

转载自www.cnblogs.com/wkwzn2019/p/13167327.html