split():
当函数默认为空,它会把所有空格(空格符、制表符、换行符)当作分隔符.
但是当函数参数默认为其他,将会对 空格符、制表符、换行符 当作元素进行分割.
a = 'hello world!' print a.split() a = 'hello world!' \ '' print a.split() a = 'hello world! ' \ '' print a.split(' ')
相对应的输出结果
['hello', 'world!']
['hello', 'world!']
['hello', 'world!', '']
第三个a中的换行符,被当作新元素进行了分割.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
print a.split(' ') print filter(None, a.split(' '))
输出结果为:
['hello', 'world!']
filter(...)
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
注意绿字部分:
感谢博主.SZlibraco
原链接:
http://www.cnblogs.com/librasz/p/3232611.html