Comparison of basic python split and join functions

Source: Internet
Author: User
The split and join methods 1 only process strings. 1. split and join methods

1 only processes strings. Split: split string and join connection string

2. string. join (sep): use string as the delimiter to combine all elements (represented by strings) in sep into a new string.

3. string. split (str = '', num = string. count (str): string separated by str. if num has a specified value, only the num sub-string is separated.

4. perform OS. path. splie ()/OS. path. join () on the imported OS module. it seems that the processing mechanism is different, but the function is the same.

II. split () method

The help information is as follows:

Split (...)

S.split([sep [,maxsplit]]) -> list of stringsReturn a list of the words in the string S, using sep as thedelimiter string. If maxsplit is given, at most maxsplitsplits are done. If sep is not specified or is None, anywhitespace string is a separator.

Chinese translation:

Split (...)

S. split ([sep [, maxsplit])-> List separated by strings

Returns a list of strings separated by separators (sep. If the maximum number of splits is specified, the limit ends at the maximum split. If the delimiter is not specified or is none, the default delimiter is 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']

['Hangel', 'W', 'rld ']

['Hangel', 'World']

OS. path. split ()

OS. path. split splits file names and paths by path, for example, d: \ python. ext, which can be divided into ['d: \ python ', 'python.exe'], for example:

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 join ()

A = 'abc' print '. '. join (a) print '| '. join (['A', 'B', 'C']) # ['A', 'B', 'C'] can be considered as a = 'abc '; print '. '. join ({'a': 1, 'B': 2, 'C': 3, 'D': 4 })

Note: '.' is used as a separator to concatenate all elements (strings) in join into a new string through a separator.

Someone may chew words like me, targeting string. the definition of join () is awesome. it converts ['A', 'B', 'C'] to a string first, and then

For example:

b=str(['a','b','c'])print '|'.join(b)

I think this is a positive solution, but it is not. The output result is: [| '| a |' |, | '| B |' |, | '| c |' |], the reason for inconsistency is that the image is superfluous and ['A', 'B', 'C'] is converted into a string. in Python, we found that the string, ancestor, and list are sequential and have the same access method. the following labels can be used to access the elements.

You can try again later.

Input:

Output:

a.b.c.da|b|ca.c.b.dos.path.join(path1[,path2[,......]])os.path.join(path1[, path2[, ...]])
# If multiple paths are combined, 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/A','/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.