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

Source: Internet
Author: User

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 a string into a list, the needs of friends can refer to the following

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 filename are given, the output path and file name
    3. If a directory name is given, the output path and the empty file name


Second, examples
1. Common examples

>>> u = "www.doiido.com.cn" #使用默认分隔符 >>> print U.split () [' www.doiido.com.cn '] #以 "." For separators >>> print u.split ('. ') [' www ', ' doiido ', ' com ', ' cn '] #分割0次 >>> print u.split ('. ', 0) [' www.doiido.com.cn '] #分割一次 >>> print U.split ('. ', 1) [' www ', ' doiido.com.cn '] #分割两次 >>> print u.split ('. ', 2) [' www ', ' doiido ', ' com.cn '] #分割两次, And take a sequence of 1 items >>> print u.split ('. ', 2) [1]doiido #分割最多次 (actual with no num parameter) >>> print U.split ('. ', -1) [' www ', ' Doiido ', ' com ', ' cn '] #分割两次 and save the segmented three sections to three files >>> u1,u2,u3 = U.split ('. ', 2) >>> print u1www>> > Print u2doiido>>> Print u3com.cn


2. Remove newline characters

>>> c = ' Sayhellobaby ' >>> print csayhellobaby >>> print c.split (' \ n ') [' say ', ' hello ', ' ba By ']

3. Separating the file name and path

>>> Import os>>> Print os.path.split ('/dodo/soft/python/') ('/dodo/soft/python ', ') >>> Print os.path.split ('/dodo/soft/python ') ('/dodo/soft ', ' python ')

4, a Super good example

>>> str= "Hello Boy<[www.doiido.com]>byebye" >>> print Str.split ("[") [1].split ("]") [0] www.doiido.com >>> Print Str.split ("[") [1].split ("]") [0].split (".") [' www ', ' doiido ', ' com ']

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.