程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Explain the usage of the join() function in Python

編輯:Python

function :string.join()

Python There is join() and os.path.join() Two functions , The specific functions are as follows :
    join():    Connection string array . The string 、 Tuples 、 Elements in the list with the specified characters ( Separator ) The connection generates a new string
    os.path.join():  Combine multiple paths and return

One 、 Function description
1、join() function

grammar :  'sep'.join(seq)

Parameter description
sep: Separator . Can be null
seq: The sequence of elements to connect 、 character string 、 Tuples 、 Dictionaries
The above grammar is : With sep As a separator , take seq All the elements are combined into a new string

Return value : Returns a separator sep String generated after connecting elements

2、os.path.join() function

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

Return value : Combine multiple paths and return

notes : Parameters before the first absolute path will be ignored

Two 、 example

# Operate on the sequence ( Separate use ' ' And ':' As a separator )
>>> seq1 = ['hello','good','boy','doiido']
>>> print ' '.join(seq1)
hello good boy doiido
>>> print ':'.join(seq1)
hello:good:boy:doiido
# Operate on strings
>>> 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
# Operate on tuples
>>> seq3 = ('hello','good','boy','doiido')
>>> print ':'.join(seq3)
hello:good:boy:doiido
# Operate the dictionary
>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print ':'.join(seq4)
boy:good:doiido:hello
# Merge catalog
>>> import os
>>> os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'

Reproduced in :https://www.jb51.net/article/63598.htm


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved