The split (), Os.path.split () function usage in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.