An example of how the split () function in Python is used

Source: Internet
Author: User

Summary:

This article mainly introduces the use of the split () function in Python, the use of the split () function is the basic knowledge of Python learning, usually used to slice and convert strings into lists, and the need for friends to refer to.

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:

    • Path refers to the full path of a file as a parameter:
    • If a directory and filename are given, the output path and file name
    • If a directory name is given, the output path and the empty file

Second, examples
1. Common examples

U ="www.doiido.com.cn"#Use default delimiterPrint(U.split ()) ['www.doiido.com.cn']#with "." As a delimiterPrint(U.split ('.'))['www','Doiido','com','cn']#Split 0 TimesPrint(U.split ('.', 0)) ['www.doiido.com.cn']#Split oncePrint(U.split ('.', 1))['www','doiido.com.cn']#split once to take the preceding characterPrint(U.split ('.', 1) [0]) www#split once, take back charactersPrint(U.split ('.', 1) [1]) doiido.com.cn#split most times (actually same as no num parameter)Print(U.split ('.',-1))['www','Doiido','com','cn']#split two times and save the divided three sections to three filesU1, U2, U3 = U.split ('.', 2)Print(U1) www .Print(U2) DoiidoPrint(U3) com.cn

2. Remove newline characters

" " Sayhellobaby " " Print (C.split ('\ n')) ['say'Hello  "baby"

3. Separating the file name and path

ImportOS#split/dodo/soft/python/to/dodo/soft/python and empty pathsPrint(Os.path.split ('/dodo/soft/python/'))('/dodo/soft/python',"')#split/dodo/soft/python to/dodo/soft and PythonPrint(Os.path.split ('/dodo/soft/python'))('/dodo/soft','python')

4, a Super good example

 str =  hello boy<[https:// Www.cnblogs.com/forcheny]>byebye   "  Print  (str.split ( " [ " ) [1].split ( "  Span style= "COLOR: #800000" "") [0].split ( "   "   " https:/ /www   ", "   Cnblogs   ", "   Com/forcheny   "] 

An example of how the split () function in Python is used

Related Article

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.