One, Python split () slices the string by specifying a delimiter, and separates only the NUM substring if the parameter num has a specified value
Str. Split(str="", num=string. Count(str)).
Parameters:STR--the delimiter, which defaults to all null characters, including spaces, line breaks (\ n), tabs (\ t), and so on. Num--the number of splits. return Value:returns the segmented list of strings. split (): Splits the string. Slices a string by specifying a delimiter and returns a segmented list of strings
Sentence= "All good things come to those who wait." #分隔符以空格print ("Delimiter with Space:", Sentence.split (")) print () #分隔符以空格, split 1 times print (" Separator with space, split 1 times: ", Sentence.split (', 1)) print () #分隔符以空格, split 2 times print ("Separator with a space, split 2 times:", Sentence.split (", 2)) print () #分隔符以空格, split 2 times, and take a sequence of 1 items print (" Separator with a space, split 2 times, and take a sequence of 1 items: ", Sentence.split (', 2) [1])
The results of the operation are as follows:
Second,Os.path.split (): Split the file name and path by Path
Os.path.split (' path ')
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
Import Os#os.path.split () returns the path and filename of the file Fname,fename=os.path.split ("e:/lpthw/zedshaw/ex19.py") Print (f "" " Os.path.split () returns the path to the file and the filename {fname}{fename} "" ") print () #os. Path.splitext () separates the file name and extension fname,fename=os.path.splitext ('/home/ubuntu/python_coding/split_func/split_function.py ') print (f "" "Os.path.splitext () separate the file name and extension {fname}{ Fename} "" ")
The results of the operation are as follows:
The split (), Os.path.split () function usage in Python