Home  >  Article  >  Backend Development  >  How to use join in python

How to use join in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-26 09:22:054147browse

Usage of the join() function in Python. The join() function is mainly used to splice strings. Function: string.join().

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

How to use join in python

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 connected.
The above syntax is: use sep as the separator to combine all elements of seq into a new string.

Return value: Returns a string generated by connecting each element with the delimiter sep.

Related recommendations: "Python Video Tutorial"

2. os.path.join() function

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

Return value: Return after combining multiple paths.

Note: Parameters before the first absolute path will be ignored.

2. Example

Operate the sequence (use ' ' and ':' as separators respectively)

>>> seq1 = ['hello','good','boy','doiido']
>>> print ' '.join(seq1)
hello good boy doiido
>>> print ':'.join(seq1)
hello:good:boy:doiido

Operation on string

>>> seq2 = "hello good boy doiido"
>>> print ':'.join(seq2)
h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o

Operation on tuple
##

>>> seq3 = ('hello','good','boy','doiido')
>>> print ':'.join(seq3)
hello:good:boy:doiido

Operation on dictionary

>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print ':'.join(seq4)
boy:good:doiido:hello

Merge Directory

>>> import os
>>> os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'

The above is the detailed content of How to use join in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn