Usage of join() function in Python

Function: string.join()

There are two functions, join() and os.path.join(), in Python. The specific functions are as follows:
    join(): Concatenate string arrays. Concatenate elements in strings, tuples, and lists with specified characters (delimiters) to generate a new string
    os.path.join(): Return after combining multiple paths

1. Function description
1. join() function

Syntax: 'sep'.join(seq)

Parameter description
sep: Separator. Can be empty
seq: element sequence, string, tuple, dictionary to be concatenated The
above syntax is: with sep as the delimiter, combine all elements of seq into a new string

Return value: Returns a string generated by concatenating the elements with the separator sep

 

2. os.path.join() function

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

Return value: Return after combining multiple paths

Note: Arguments before the first absolute path will be ignored

1 #Operate on the sequence (use '  ' and ' : ' as separators respectively)
 2    
3 >>> seq1 = [ ' hello ' , ' good ' , ' boy ' , ' doiido ' ]
 4 >>> print '  ' .join(seq1)
 5  hello good boy doiido
 6 >>> print ' : ' .join(seq1)
 7  hello:good:boy:
doiido 8    
9    
10 #Operate  on strings
 11    
12 >>> seq2 = "hello good boy doiido"
13 >>> print ':'.join(seq2)
14 h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o
15   
16   
17 #对元组进行操作
18   
19 >>> seq3 = ('hello','good','boy','doiido')
20 >>> print ':'.join(seq3)
21 hello:good:boy:doiido
on the dictionary
242322   
   
 25   
26 >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
27 >>> print ':'.join(seq4)
28 boy:good:doiido:hello
29   
30   
31 #合并目录
32   
33 >>> import os
34 >>> os.path.join('/hello/','good/boy/','doiido')
35 '/hello/good/boy/doiido'

 

 

Excerpted from the blog post: https://www.cnblogs.com/jsplyy/p/5634640.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325688348&siteId=291194637