The split () function and the Os.path.split () function in Python

Source: Internet
Author: User
Tags in python

There are split () and Os.path.split () two functions in Python:

Split (): Split string. Slices the string by specifying a delimiter, and returns a list of the separated strings.

Os.path.split (): Splits the file name and path.

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 an empty string. If there are no delimiters in the string, the entire string is taken as an element of the list.
Num: Indicates the number of splits. If parameter num exists, it is separated into num+1 substrings only.
[n]: To select the nth fragment (count from 0)

By default, spaces are used as delimiters, and when separated, empty strings are automatically ignored, such as:

>>> s= ' Love python '

>>> S.split ()
[' Love ', ' python ']

However, if you explicitly specify that a space is a separator, empty strings are not automatically ignored, such as:

>>> S.split (")
[' Love ', ', ', ', ', ' Python ']

The default separator, except for spaces, and ' \n\t\r ', is separated, and the empty string is automatically ignored as follows:

>>> s= ' love \n\t\r \t\r\n python \n\t\r '
>>> S.split ()
[' Love ', ' python ']


>>> s= ' www.pku.edu.cn '
>>> s.split () #默认空格作为分隔符, but there are no delimiters in the string, so take the entire string as an element of the list
[' www.pku.edu.cn ']

>>> s.split ('. ') #以 '. ' As a delimiter, there are no separate times, and how many '. ' Are separated
[' www ', ' pku ', ' edu ', ' CN ']

>>> s.split ('. ', 0) #分隔0次
[' www.pku.edu.cn ']

>>> s.split ('. ', 1) #分隔1次
[' www ', ' pku.edu.cn ']

>>> s.split ('. ', 2) #分隔2次
[' www ', ' pku ', ' edu.cn ']

>>> s.split ('. ', 2) [1] #分隔2次, items indexed as 1
' PKU '

>>> s.split ('. ', -1) #尽可能多的分隔, same as no num parameter
[' www ', ' pku ', ' edu ', ' CN ']

>>> s1,s2=s.split ('. ', 1) #分隔1次 and store the separated 2 strings in S1 and S2
>>> S1
' www '
>>> S2
' Pku.edu.cn '

>>> s= ' Love
... hello
... python '
>>> s
' Love\nhello\npython '
>>> s.split (' \ n ') #以 ' \ n ' as a separator, as many times as possible
[' Love ', ' hello ', ' python ']
>>> Print S
Love
Hello
Python

Practice the following example:

>>> s= ' Hello Python<[www.python.com]>hello python '
>>> s.split (' [') [1].split ('] ') [0]
' Www.python.com '
>>> s.split (' [') [1].split ('] ') [0].split ('. ')
[' www ', ' python ', ' com ']


2, Os.path.split () function
Syntax: Os.path.split (' path ')

Parameter description: path refers to the full path of a file as an argument: if the given is a directory and file name, then the output path and file name if given a directory name, then the output path and the empty file name in fact, the function's segmentation is not intelligent, it is only "PATH" in the Last '/' as a separator, After separating, treat the index as a directory (path) of 0 and treat the index 1 as a filename, such as:

>>> Import OS
>>> os.path.split (' c:/soft/python/test.py ')
(' C:/soft/python ', ' test.py ')
>>> os.path.split (' c:/soft/python/test ')
(' C:/soft/python ', ' test ')
>>> os.path.split (' c:/soft/python/')
(' C:/soft/python ', ')

Finish

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.