The use of the split () function in Python

Source: Internet
Author: User

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

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.