5.split
语法:str.split(sep=None,maxsplit = -1)
或str.split(sep=None,maxsplit = -1)[n]
解释:拆分str,通过指定的sep对str进行分割,并返回分割后的字符串列表
sep为分隔符,默认为所有空字符,包括空格(‘ ’),换行(\n),制表符(\t),但不可为空,若字符串中没有sep,则把整个字符串作为列表的一个元素;
maxsplit为分割次数,默认为-1(分割所有)。
n为索引,返回值为list,可以执行 L[n].
第一种情况:默认
>>> x = 'with a moo-moo here, and a moo-moo there'
>>> x.split()
['with', 'a', 'moo-moo', 'here,', 'and', 'a', 'moo-moo', 'there']
第二种情况:指定分割
>>> x = 'with a moo-moo here, and a moo-moo there'
>>> x.split('moo'