Python Basics Os.path.join and Split () functions

Source: Internet
Author: User

Python has join and Os.path.join () two functions, which work as follows:

Join: An array of connection strings. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection
Os.path.join (): Combine multiple paths to return

First, function description

1.join () function

Syntax: ' Sep '. Join (SEQ)

Parameter description:

Sep: Delimiter. Can be empty

SEQ: sequence of elements to concatenate, strings, tuples, dictionaries, etc.

The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter

Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep

2. Os.path.join () function

Syntax: Os.path.join (path1[,path2[,......])

Return value: Combine multiple paths to return

Note: Parameters before the first absolute path are ignored

Instance:

#对序列进行操作 (use ' and ': ' as separators) >>> seq1 = [' Hello ', ' good ', ' boy ', ' Doiido ']>>> print '. Join (SEQ1) Hello Good boy doiido>>> print ': '. Join (seq1) Hello:good:boy:doiido #对字符串进行操作 >>> seq2 = "Hello good boy doiid O ' >>> print ': '. Join (SEQ2) H:e:l:l:o:: g:o:o:d:: b:o:y::d: o:i:i:d:o #对元组进行操作 >>> seq3 = (' Hello ', ' good ', ' Boy ', ' Doiido ') >>> print ': '. Join (SEQ3) Hello:good:boy:doiido #对字典进行操作 >>> seq4 = {' Hello ': 1, ' good ': 2, ' Boy ': 3, ' Doiido ':4}>>> print ': '. Join (seq4) Boy:good:doiido:hello #合并目录 >>> Import os>> > Os.path.join ('/hello/', ' good/boy/', ' doiido ') '/hello/good/boy/doiido '

There is also a code for the 99 multiplication table on Oschina, as follows (source link: http://www.oschina.net/code/snippet_53549_2238)

print   \ n ' .join ([ .join ([ '%s*%s=%-2s '  %   (y,x,x * y)   for   y   in   range ( 1 ,x + 1 for   < Code class= "Plain" >x  in  range ( 1 10 Code class= "Plain")])

短短一句话,就完美打印出99 multiplication table.

The appreciation of this Code, Baidu blog has an article about the very clear, not much to say in this.

The original text is as follows: (Link: http://hi.baidu.com/fc_lamp/blog/item/fb7d410bf7314c0295ca6b8c.html)

An analysis of a Python code born 99 multiplication table 2011-11-24 11:58

The code for the section 99 multiplication table is recently seen on Oschina, as follows:

print ' \ n '. Join (['%s*%s=%-2s '% (y,x,x*y) fory in range (1,x+1)]) Forx in range (1,10)])

(As for: http://www.oschina.net/code/snippet_53549_2238)

I tweaked a little bit:

#coding: Utf-8
Print (' \ n '. Join ('%sx%s=%-2s '% (x,y,x*y) for x in Xrange (1,y+1)) "For Y in Xrange (1,10)]))

Well, in fact, the two code are not much, then we will parse, this code:

First: The main points of knowledge to be used:

1 List parsing knowledge (and meta-group knowledge)

2 Use of Range,xrange,join functions

3 String formatted output

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

Split (): Splits the string. Slices a string by specifying a delimiter and returns a list of the split 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: Represented as a delimiter, the default is a space, but not an empty string. 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.
[n]: Indicates the selection of nth shards (counted from 0)

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

>>> s= ' love    python ' >>> s.split () [' Love ', ' python ']

However, if you explicitly specify a space as a delimiter, an empty string is not automatically ignored, such as:

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

The default delimiter, in addition to 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 is no delimiter in the string, so the entire string is used as an element of the list
[' www.pku.edu.cn ']

>>> s.split ('. ') #以 '. ' As a delimiter, without specifying the number of separators, the number of '. ' is separated by how many times
[' 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次, take an index of 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

' Love\nhello\npython '
>>> s.split (' \ n ') #以 ' \ n ' as a delimiter with as many separators 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:

    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
In fact, the segmentation of the function is not intelligent, it is only the "path" in the last '/' as a delimiter, separated by the index of 0 as a directory (path), the index of 1 as a file name, 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 ', ')

Python basic knowledge os.path.join and Split () function

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.