In Python join()
method may be elements of a sequence to the specified character string to generate a new connection. The sequence may be a string, a tuple, list or dictionary
join () method Syntax: 'str'.join(sequence)
wherein STR : separators, can be empty; Sequence : a sequence of elements to be connected
The string operations:
>>> sequence = 'I Love Python'
>>> print('.'.join(sequence))
I. .L.o.v.e. .P.y.t.h.o.n
Tuple operation:
>>> sequence = ('I', 'Love', 'Python')
>>> print(''.join(sequence))
ILovePython
>>> sequence = ('I', 'Love', 'Python')
>>> print('-'.join(sequence))
I-Love-Python
Operating the list:
>>> sequence = ['I', 'Love', 'Python']
>>> print(''.join(sequence))
ILovePython
>>> sequence = ['I', 'Love', 'Python']
>>> print('+'.join(sequence))
I+Love+Python
Dictionary of operation (only dictionary key connection):
>>> sequence = {'a':1, 'b':2, 'c':3, 'd':4}
>>> print(''.join(sequence))
abcd
>>> sequence = {'a':1, 'b':2, 'c':3, 'd':4}
>>> print('_'.join(sequence))
a_b_c_d
In Python there is a os.path.join()
method that belongs to the os.path
module, the effect of the method of splicing one or more paths, it is necessary to note the following:
- If the first letter of each component name is not included
\
, the function will automatically add - If the last component is null, then the generated path to a
\
separator end - If a component is an absolute path, all components before it will be discarded
Case 1: the first letter of each component name is not included \
>>> import os
>>>
>>> Path1 = 'home'
>>> Path2 = 'language'
>>> Path3 = 'python'
>>>
>>> Path = os.path.join(Path1, Path2, Path3)
>>> print('Path =', Path)
Path = home\language\python
Case 2: The final component is empty
>>> import os
>>>
>>> Path1 = 'home'
>>> Path2 = 'language'
>>> Path3 = ''
>>>
>>> Path = os.path.join(Path1, Path2, Path3)
>>> print('Path =', Path)
Path = home\language\
Case three: one component is an absolute path
>>> import os
>>>
>>> Path1 = 'home'
>>> Path2 = '\language'
>>> Path3 = 'python'
>>> Path = os.path.join(Path1, Path2, Path3)
>>> print('Path =', Path)
>>> Path = \language\python