function:split ()
There are two functions for split () and Os.path.split () in Python, as follows:
Split (): Splits the string. Slices a string by specifying a delimiter and returns a segmented list of strings
Os.path.split (): Splits the file name and path by path
First, function description
1. Split () function
Syntax:str.split (str= "", Num=string.count (str)) [n]
Parameter description:
STR: is represented as a delimiter, the default is a space, but cannot be empty ('). If there is no delimiter in the string, the entire string is used as an element of the list
Num: Indicates the number of splits. If there is a parameter num, it is separated into only num+1 substrings, and each substring can be assigned to a new variable
[n]: Indicates selection of Nth Shard
Note: When spaces are used as separators, items that are empty in the middle are automatically ignored
2. Os.path.split () function
Syntax:os.path.split (' path ')
Parameter description:
1.PATH refers to the full path of a file as a parameter:
2. If a directory and file name is given, the output path and filename
3. If a directory name is given, the output path and the empty file name
#.split () Cutting
Separating Strings"."
# s = "www.sina.com.cn"
# S1 = S.split (".") #点分割 all split
# print (S1) # turn string into list [' www ', ' sina ', ' com ', ' CN ']
cut two times
# S1 = S.split (".", 2) # split 2 times
# print (S1) # [' www ', ' sina ', ' com.cn ']
split two times and take the sequence as1 of items
# S1 = S.split (".", 2) [1] #分隔两次, take out the first item
# print (S1) # Sina
split two times and save the divided three sections to three files
# S1,S2,S3 = S.split (".", 2)
# print (S1) # www
# print (S2) #sina
# print (S3) #com. cn
Cut the end of the cutting head to the middle
# s = "Hello Boy<[www.baidu.com]>byebye" #切头切尾, take a
# Print (S.split ("[") [1].split ("]") [0]) # www.baidu.com
The use of the split () function in Python