【详解】python中字符串的strip(),lstrip(),rstrip()的含义
2017-07-05 10:45
756 查看
【解答】
1. 参考,你自己给的,python官网的解释:
http://docs.python.org/2/library/string.html?highlight=strip#string.lstrip
主要是:
其中很明显的是:
针对strip的详细解释,翻译过来,就是:
对于输入的字符串,去除,开始的,和,末尾的,字符;
所要去除的字符,是你通过参数chars所指定的。
如果不指定该参数,或者为None,
(即形式为:
或
或
)
则默认为白空格Whitespace。
所谓的白空格,一般指的是:
空格本身,回车\r,换行\n,制表符\t, 换页符\f
(对应于正则表达式中的: \s == [ \r\n\t\f] )
所以,很明显的是:
而关于此处的:
我觉得是,其多写了个lstrip(),应该改成:
其等价于
意思是
对于name.contents[0],
先去除首尾(即头部和尾部)的白空格(空格本身,回车\r,换行\n,制表符\t, 换页符\f )
之后,再去除尾部的逗号’,’
假如,原先字符串是:
?
经过这么一处理,则很明显,去除首尾的白空格后,再去除尾部的逗号,就变成了:
?
了。
进一步举例说明:
strip一般用来是去除一个字符的首尾的多余的,不可见的字符(所谓的白空格)。
比如一个字符是:
?
那么:
?
这下明白了吧?
转载请注明:在路上 » 【详解】python中字符串的strip(),lstrip(),rstrip()的含义
1. 参考,你自己给的,python官网的解释:
http://docs.python.org/2/library/string.html?highlight=strip#string.lstrip
主要是:
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, chars must 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. |
lstrip = left strip =去除(字符串)左边的=stip leading=去除(字符串)开始的 rstrip = right strip =去除(字符串)右边的=strip trailling=去除(字符串)末尾的 strip = stip left and right = 去除(字符串的)左边的和右边的=strip leading and trailing = 去除(字符串)开始的和末尾的 |
针对strip的详细解释,翻译过来,就是:
对于输入的字符串,去除,开始的,和,末尾的,字符;
所要去除的字符,是你通过参数chars所指定的。
如果不指定该参数,或者为None,
(即形式为:
someStringValue.strip() |
someStringValue.strip(None) |
someStringValue.strip(chars=None) |
则默认为白空格Whitespace。
所谓的白空格,一般指的是:
空格本身,回车\r,换行\n,制表符\t, 换页符\f
(对应于正则表达式中的: \s == [ \r\n\t\f] )
所以,很明显的是:
someString.strip() == someString.lstrip().rstrip() |
而关于此处的:
name.contents[0].strip() .lstrip() .rstrip(‘,’) |
name.contents[0].strip().rstrip(‘,’) |
name.contents[0].strip(None).rstrip(‘,’) |
对于name.contents[0],
先去除首尾(即头部和尾部)的白空格(空格本身,回车\r,换行\n,制表符\t, 换页符\f )
之后,再去除尾部的逗号’,’
假如,原先字符串是:
?
?
进一步举例说明:
strip一般用来是去除一个字符的首尾的多余的,不可见的字符(所谓的白空格)。
比如一个字符是:
?
?
转载请注明:在路上 » 【详解】python中字符串的strip(),lstrip(),rstrip()的含义
相关文章推荐
- python字符串处理rstrip()、lstrip()、strip()函数使用
- 浅谈python中截取字符函数strip,lstrip,rstrip
- Python中strip(),lstrip(), rstrip()使用方法
- 浅谈python中截取字符函数strip,lstrip,rstrip
- Python strip() lstrip() rstrip() 函数 去除空格
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
- python中strip(),lstrip(),rstrip()函数的讲解
- python strip() lstrip() rstrip() 使用方法
- Python中的strip()和lstrip()以及rstrip()方法的使用
- python——strip,lstrip和rstrip
- python的strip(),lstrip()和rstrip()
- python:strip()、rstrip()、lstrip()
- Python误区之strip,lstrip,rstrip
- Python-strip(),lstrip(),rstrip()
- Python strip、lstrip和rstrip的用法
- python中strip,lstrip,rstrip简介
- Python中的strip(),lstrip(),rstrip()的用法
- Python 3.x 字符串的makestrans函数和translate方法使用详解
- 字符串strip()方法注意事项 分类: python基础学习 2013-08-16 12:01 422人阅读 评论(0) 收藏
- Python 字符串方法详解