Function: split ()
Python, the split () and The os.path.split () two functions, here briefly split () function:
split (): Split string. Slicing string specified by delimiters and returns the string list (list) divided
First, Function Description
1, split () function
Syntax: str.split (str = "", num = string.count (str)) [n]
Parameters:
STR: expressed as a separator, a space by default, but can not be empty ( ''). If no delimiter string, the whole string as put a list of elements
num: indicates the segment number. If the parameter num is present, only num + 1 is divided into sub-strings, each sub-string and can be assigned to a new variable
[n]: represents the n-th slice select
NOTE: When using space as delimiter, the intermediate empty items are automatically ignored.
Second, the isolated string
string = "www.gziscas.com.cn"
1. '' is a delimiter
print(string.split('.'))
[ 'Www', 'gziscas', 'with', 'cn']
2. split twice
print(string.split('.',2))
[ 'Www', 'gziscas', 'com.cn']
3. The split twice, and taking a sequence of items 1
print(string.split('.',2)[1])
gziscas
4. split twice and saves the three portions divided into three files
u1, u2, u3 =string.split('.',2)
print(u1)—— www
print(u2)—— gziscas
print (U3) --com.cn
Third, complex examples
str="hello boy<[www.baidu.com]>byebye"
print(str.split("[")[1].split("]")[0])
www.baidu.com