API文档:
string.split(s[,sep[,
maxsplit]])
-
Return a list of the words of the string s. If the optional second argumentsep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argumentsep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. The optional third argument maxsplit defaults to 0. If it is nonzero, at mostmaxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at mostmaxsplit+1 elements).
The behavior of split on an empty string depends on the value ofsep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string.
翻译文档: - 参数: s:目标字符串
- sep:分割字符串 maxsplit:
- 描述: 该方法返回一个s字符串的分割后的list。
- 如果可选项的第二个参数sep不填写,则目标字符串根据空白字符(space, tab, newline, return, formfeed)进行分割. 如果第二个参数填写,则使用该字符串进行字符串分割。
- 如果第三个参数填写,则匹配次数 例子:#! /usr/bin/env python#coding=utf-8
print string.split(str)print string.split(str,'a')print string.split(str,' ',1)print string.split(str,' ',2)print string.split(str,'sdf')输出:['a', '<nd', 'abc', '<nd', 'nd', '>']['', ' <nd ', 'bc <nd nd > ']['a', '<nd abc <nd nd > ']['a', '<nd', 'abc <nd nd > ']['a <nd abc <nd nd > ']
import string
str="a <nd abc <nd nd > "