Python base Split and join function comparison

Source: Internet
Author: User
First, about the split and join methods

1 is processed only for strings. Split: Split string, join connection string

2.string.join (Sep): Combines all elements of Sep (string representations) into a new string, using string as a separator

3.string.split (str= ", Num=string.count (str)): delimited by str, the character slices a string, and if NUM has a specified value, only the NUM substring is delimited.

4. Os.path.splie ()/os.path.join () on the Import OS module seems to be a different processing mechanism, but functionally identical.

Second, Split () method

The information after help is as follows:

Split (...)

S.split ([Sep [, Maxsplit]]), List of Stringsreturn a list of the words in the string S, using Sep as Thedelimiter stri Ng. If Maxsplit is given, at the most maxsplitsplits be done. If Sep is no specified or is None, anywhitespace string is a separator.

Chinese translation:

Split (...)

S.split ([Sep [, Maxsplit]]), a list of strings divided into

Returns a set of lists that are formed by using a separator (SEP) Split string. If you specify the maximum number of splits, the end is the maximum split. If the delimiter is not specified or is none, the delimiter defaults to a space.

Instance:

S= ' a b C ' Print s.split (') st= ' Hello World ' print st.split (' o ') print st.split (' O ', 1)

--------Output---------

[' A ', ' B ', ' C ']

[' Hell ', ' w ', ' Rld ']

[' Hell ', ' World ']

Os.path.split ()

Os.path.split is separated by the path of the file name and path, such as D:\\python\\python.ext, can be divided into [' D:\\python ', ' Python.exe '], the example is as follows:

Import Osprint os.path.split (' C:\\Program File\\123.doc ') print Os.path.split (' C:\\Program file\\ ')----------------- Output---------------------(' C:\\Program file ', ' 123.doc ') (' C:\\Program file ', ')

Three joins ()

A= ' ABCD ' print '. Join (a)  print ' | '. Join ([' A ', ' B ', ' C ']) #可以把 [' A ', ' B ', ' C '] as a= ' ABCD '; Join ({' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4})

Note: '. ' Wait for the delimiter to concatenate all the elements (strings) in the join into a new string through the delimiter

Maybe someone like me, the definition of String.Join () Love dead, abruptly to [' A ', ' B ', ' C '] first converted to a string, and then in the join

Such as:

B=str ([' A ', ' B ', ' C ']) print ' | '. Join (b)

I thought it was a positive solution, but otherwise. The output is: [| ' | a| ' |,| | b| ' |,| | C| ' |], and the cause of the inconsistency with the above is the lily, the [' A ', ' B ', ' C '] into a string, in Python, we found that the string, Ganso, list They are sequence types, have the same access, you can access the following index elements.

The above can be knocked again to try again.

Input:

Output:

A.b.c.da|b|ca.c.b.dos.path.join (path1[,path2[,......]) Os.path.join (path1[, path2[, ...])
#将多个路径组合后返回, the parameters before the first absolute path are ignored. >>> os.path.join (' c:\\ ', ' csv ', ' test.csv ') ' C:\\csv\\test.csv ' >>> os.path.join (' Windows\Temp ', ' C : \ \ ', ' csv ', ' test.csv ') ' C:\\csv\\test.csv ' >>> os.path.join ('/home/aa ', '/home/aa/bb ', '/home/aa/bb/c ') '/ Home/aa/bb/c '
  • 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.